JS Ajax calling PHP and getting ajax call data

前端 未结 3 1775
死守一世寂寞
死守一世寂寞 2021-01-24 22:19

I have a standard javascript ajax call where I\'m setting the data: to json data.

$.ajax({
    type: \"POST\",
    url: BaseUrl + \"User/Login\",    
    //url:          


        
相关标签:
3条回答
  • 2021-01-24 23:05

    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"]
    
    0 讨论(0)
  • 2021-01-24 23:12

    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)

    0 讨论(0)
  • 2021-01-24 23:24

    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.

    0 讨论(0)
提交回复
热议问题