php jQuery AJAX 类型:'GET',传值问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2407653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery AJAX type: 'GET', passing value problem
提问by Trez
I have a jQuery AJAX call with type:'GET' like this:
我有一个类型为:'GET' 的 jQuery AJAX 调用,如下所示:
$.ajax({type:'GET',url:'/createUser',data:"userId=12345&userName=test",
success:function(data){
alert('successful');
}
});
In my console output is: GET:http://sample.com/createUser?userId=12345&userName=testparams: userId 12345 userName test
在我的控制台输出是: GET: http://sample.com/createUser?userId=12345&userName=testparams: userId 12345 userName test
In my script i should get the value using $_GET['userId'] and $_GET['userName'] but i can't get the value passed in by ajax request using GET method.
在我的脚本中,我应该使用 $_GET['userId'] 和 $_GET['userName'] 获取值,但是我无法使用 GET 方法获取 ajax 请求传入的值。
Any ideas on how to do this?
关于如何做到这一点的任何想法?
thanks,
谢谢,
回答by Quentin
The only thing I can see wrong with the code (which no longer applies as the question has been edited (which suggests that the code has been rewritten for the question and might not accurately reflect the actual code in use)) is that the success function is in the wrong place.
我唯一能看出代码有问题(随着问题被编辑而不再适用(这表明代码已针对问题重写,可能无法准确反映正在使用的实际代码))是成功函数是在错误的地方。
You have:
你有:
$.ajax(
{
type:'GET',
url:'/createUser',
data:"userId=12345&userName=test"
},
success: function(data){
alert('successful');
}
);
which should be:
应该是:
$.ajax(
{
type:'GET',
url:'/createUser',
data:"userId=12345&userName=test",
success: function(data){
alert('successful');
}
}
);
Despite this, your description of the console output suggests the data is being sent correctly. I'd try testing with this script to see what PHP actually returns (you can see the body of the response in the Firebug console):
尽管如此,您对控制台输出的描述表明数据正在正确发送。我会尝试使用此脚本进行测试以查看 PHP 实际返回的内容(您可以在 Firebug 控制台中看到响应的正文):
<?php
header("Content-type: text/plain");
print_r($_REQUEST);
?>
回答by Patxi1980
The url is missing the extension, and you can pass data as an object, so jquery convert it to a string
url缺少扩展名,可以将数据作为对象传递,因此jquery将其转换为字符串
$.ajax({
type:'GET',
url:'/createUser.php',
data:{"userId":"12345","userName":"test"},
success:function(data){
alert('successful');
}
);

