jQuery的$。阿贾克斯()与PHP [英] jQuery $.ajax() with PHP

查看:171
本文介绍了jQuery的$。阿贾克斯()与PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery $。阿贾克斯(),但我现在面临一些困难。

I'm trying to use jQuery $.ajax() but I'm facing some difficulties.

下面是我想使用POST的文本字段:

Here's the textbox field that I want to use for POST:

<input name="url" class="url" type="text" >

这里的code:

Here's the code:

$.ajax({
        type: "post",
        url: "file.php",
        data: $(this).serialize(),
        success: function(data) { ...............

现在,这是为file.php:

Now this is the file.php:

<?php
if( $_REQUEST['url'] )
{

   $url = $_REQUEST['url'];
   $url = file_get_contents($url);  
   // I would need now to return something here but not sure how??!!
}
?>

现在,我的问题是,如何在这个PHP code返回变量,并利用它们在我的code以上,我的意思是在$阿贾克斯()成功的一部分。另外,如果我想在$ url变量执行一些其他的东西,怎么办呢?如何回报他们? :/

Now, my question is, how to return variables in this PHP code and use them in my code above, I mean in the success part of $.ajax(). Also if I want to perform some additional stuff on the $url variable, how to do it? How to return them? :/

推荐答案

您只需打印/呼应的回归的价值。

You just print/echo your 'return' value.

为file.php

<?php
if( $_REQUEST['url'] )
{

   $url = $_REQUEST['url'];
   $url = file_get_contents($url);  
   // I would need now to return something here but not sure how??!!
   echo "something";
}
?>

然后在您的JS:

Then in your JS:

$.ajax({
    type: "post",
    url: "file.php",
    data: $(this).serialize(),
    success: function(data) {
        console.log(data); // "something"
    }
});


作为一个方面说明。您的脚本看起来像它接受任何URL,并获取它。是可能的滥用那样脚本。请确保您知道。


As a side note. Your script looks like it accepts any URL and fetches it. It is possible to abuse scripts like that. Make sure you are aware that.

这篇关于jQuery的$。阿贾克斯()与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆