jQuery AJAX Call to PHP Script with JSON Return

后端 未结 5 459
孤城傲影
孤城傲影 2020-11-27 04:40

I\'ve been smashing my head against a brick wall with this one, i\'ve tried loads of the solutions on stackoverflow but can\'t find one that works!

Basically when I

相关标签:
5条回答
  • 2020-11-27 04:55

    Use parseJSON jquery method to covert string into object

    var objData = jQuery.parseJSON(data);
    

    Now you can write code

    $('#result').html(objData .status +':' + objData .message);
    
    0 讨论(0)
  • 2020-11-27 04:58

    Make it dataType instead of datatype.

    And add below code in php as your ajax request is expecting json and will not accept anything, but json.

    header('Content-Type: application/json');
    

    Correct Content type for JSON and JSONP

    The response visible in firebug is text data. Check Content-Type of the response header to verify, if the response is json. It should be application/json for dataType:'json' and text/html for dataType:'html'.

    0 讨论(0)
  • 2020-11-27 05:01

    I recommend you use:

    var returnedData = JSON.parse(data);
    

    to convert the JSON string (if it is just text) to a JavaScript object.

    0 讨论(0)
  • 2020-11-27 05:05

    try to send content type header from server use this just before echoing

    header('Content-Type: application/json');
    
    0 讨论(0)
  • 2020-11-27 05:09

    Your datatype is wrong, change datatype for dataType.

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