jQuery AJAX类型:"GET",传递值问题 [英] jQuery AJAX type: 'GET', passing value problem
问题描述
我有一个类型为"GET"的jQuery AJAX调用:
I have a jQuery AJAX call with type:'GET' like this:
$.ajax({type:'GET',url:'/createUser',data:"userId=12345&userName=test",
success:function(data){
alert('successful');
}
});
在我的控制台中输出是: GET: http://sample.com/createUser?userId=12345&userName=test 参数:userId 12345 用户名测试
In my console output is: GET:http://sample.com/createUser?userId=12345&userName=test params: userId 12345 userName test
在我的脚本中,我应该使用$ _GET ['userId']和$ _GET ['userName']来获取值,但我无法使用GET方法获取由ajax请求传递的值.
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.
关于如何执行此操作的任何想法?
Any ideas on how to do this?
谢谢
推荐答案
我唯一看到的代码错误(由于问题已被编辑,因此不再适用(这表明该问题的代码已被重写)并且可能无法准确反映所使用的实际代码))是成功函数位于错误的位置.
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.
您有:
$.ajax(
{
type:'GET',
url:'/createUser',
data:"userId=12345&userName=test"
},
success: function(data){
alert('successful');
}
);
应为:
$.ajax(
{
type:'GET',
url:'/createUser',
data:"userId=12345&userName=test",
success: function(data){
alert('successful');
}
}
);
尽管如此,您对控制台输出的描述表明数据已正确发送.我会尝试使用此脚本进行测试,以查看PHP实际返回的内容(您可以在Firebug控制台中看到响应的正文):
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
header("Content-type: text/plain");
print_r($_REQUEST);
?>
这篇关于jQuery AJAX类型:"GET",传递值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!