JS调用Ajax的PHP和获取Ajax调用数据 [英] JS Ajax calling PHP and getting ajax call data
问题描述
我有一个标准的JavaScript调用Ajax在哪里,我的数据设置:为JSON数据。
I have a standard javascript ajax call where I'm setting the data: to json data.
$.ajax({
type: "POST",
url: BaseUrl + "User/Login",
//url: BaseUrl + "User/Limit/1/2",
data: '{"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}',
success: function(data){
console.log(data);
},
error: function(request){
console.log(request);
},
});
我试图让在PHP中的数据 $ _ POST [数据]
这不起作用。
然而,数据: 'test={"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}'$c$c>作品。
I was trying to get the data in php $_POST["data"]
this doesn't work.
However, data: 'test={"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}'
works.
我不知道是不是可能我从工作的框架或类似的东西preventing $ _ POST [数据]
或者这是不可能的呢?还是有别的东西,我可以用它来获得这些数据?
I was wondering is it possibly my framework or anything like that preventing $_POST["data"]
from working or is this just not possible at all? Or is there something else I could use to get that data?
编辑:
于是框架YII和延伸Restfullyii有一个方法来获得它使用一条线的数据 返回json_de code(的file_get_contents(PHP://输入),真);
So the framework YII and the extension Restfullyii has a method to get the data it is using one line return json_decode(file_get_contents("php://input"), true);
这是越来越所有的数据,而不需要对数据进行=或{数据:不过它似乎是返回一个数组因此Im访问像$数据我特性[username的],其中真正的JSON对象应该是$数据 - > [username的。纠正我,如果我错了,在任何这我收到阵列在这种情况下,因为我真的发送一个JSON字符串?与JSON对象?
Which is getting all the data without the need for data= or {data: However it seems to be returning an array so Im accessing my properties like $data["userName"] where a true json object should be $data->["userName"]. Correct me if I'm wrong on any of this am I getting array in this case because I'm really sending a json string? versus a json object?
编辑X2:
所以PHP是使它成为一个assoc命令阵列,因为它是真正发送到json_de code ..
So php is making it an assoc array because it is sending true to the json_decode..
推荐答案
我想问题,你的code是在设置数据线:{......}
。
它应该是JSON格式,以便正确地传递(尽管它也可以是字符串格式,但你需要分析它在服务器端)
I think problem with your code is in the line where you set data: '{....}'
.
It should be in json format in order to be passed properly (though it also could be in string format but you'll need to parse it on the server side)
在code以下应工作正确的:
The code below should be working right:
$.ajax({
type: "post",
url: BaseUrl + "User/Login",
data: {"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"},
success: function(data){
console.log(data);
},
error: function(request){
console.log(request);
}
});
在服务器端尝试: $ _ POST ['apiKey']
$ _ POST ['appIDGiven']
和不久。
On the server side try: $_POST['apiKey']
$_POST['appIDGiven']
and so on.
这篇关于JS调用Ajax的PHP和获取Ajax调用数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!