开发者

Why is this ajax post giving me an error? Does an ajax post require "true" or "false" to be returned?

开发者 https://www.devze.com 2023-04-10 06:59 出处:网络
开发者_开发技巧$.ajax({ url:\"http://www.xxxxxxxxxxxxxx.com/cc/validate\", type:\"POST\", dataType:\"json\",
开发者_开发技巧$.ajax({
    url:"http://www.xxxxxxxxxxxxxx.com/cc/validate",
    type:"POST",
    dataType:"json",
    data: JSON.encode($parts),
    complete: function(){
    },
    success: function(n)
    {
        console.log(n);
        console.log(n.object);
        console.log("ajax complete");
    },
    error: function(){
        console.log("error");

    }
 });
  • This gets an array defined above the ajax post call and posts an encoded json array to a php file. The problem is determining why the post will only let me return "true" or "false". If i try to return any string, i get the error in the ajax. I want to be able to return a string created in the php and not only "true" or "false".


are you on a local host? well if so you might have to change your mime headers (application/json) ... your javascript is expecting json but your php is echoing html.

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

if you want to be able to return something else then json you have to delete or change the content type in your ajax call. the content type is for giving jquery a hint of what to expect from the server. if you tell it will receive json data you need to give it json or you'll have a parse error.


your code is alright just make sure your php file return an echo json_encode($arrayOFdata)

dataType:"json",

means the data recived from php will be parsed as a json object

it doesn't mean you will send a json object

0

精彩评论

暂无评论...
验证码 换一张
取 消