JS Ajax calling PHP and getting ajax call data

狂风中的少年 提交于 2019-12-02 07:04:51

问题


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);
    },
});

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.

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?

EDIT:

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);

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?

EDIT x2:

So php is making it an assoc array because it is sending true to the json_decode..

|improve this question

回答1:


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)

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);
    }
});

On the server side try: $_POST['apiKey'] $_POST['appIDGiven'] and so on.




回答2:


data option must be an object or serialized(e.g. "name1=value1&name2=value2") string.So you need to pass like this:

data: /*object*/{data:'{"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}'},
                // ^-----this is added for $_POST["data"]

or like:

data: /*serialized string*/'data={"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}',
                           // ^-----this is added for $_POST["data"]



回答3:


First, the data sent must be a JSON object and not a string. Remove the quotes.

Also, in your server-side, you'll better decode the input $_POST['data'] with json_decode() (see documentaion)



来源:https://stackoverflow.com/questions/11385668/js-ajax-calling-php-and-getting-ajax-call-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!