繁体   English   中英

ajax成功回调不适用于json响应

[英]ajax success callback not working on json response

AJAX请求已成功发送到php服务器,并且还在我的oracle数据库中插入了记录。 我无法成功获得JSON响应,错误,完整的回调函数。

我也看不到console.log上的任何日志

我正在使用codeigniter,而我的php服务器位于模型文件夹中,它返回json格式的字符串。

echo json_encode(array("Response"=>99,"Value"=> $result));

这是我的ajax代码:

$.ajax({
    url: 'url/functionhere',
    type: 'post',
    dataType: 'json',
    data: {
        "STUDID": studId,
            "LASTNAME": lastName,
            "FIRSTNAME": firstName,
            "MIDINITIAL": midInitial,
            "BIRTHDAY": birthday,
            "AGE": age,
            "GENDER": 'M'
    },
    success: function (data) {
        if (data.Response == 99) {
            alert(data.Value);
            console.log("putek");
        }
    },
    error: function (header, status, error) {
        console.log('ajax answer post returned error ' + header + ' ' + status + ' ' + error);

    },
    complete: function () {
        console.log("putek");
    }
});

在您的PHP文件中设置内容类型:

@header( 'Content-Type: application/json' );
echo json_encode( array( "Response" => 99, "Value" => $result ) );
exit;

您可能还想尝试一下(使用jQuery 1.9.1):

 var args=new Object();
 args["STUDID"]=studId;
 args["LASTNAME"]=lastName;
 args["FIRSTNAME"]=firstName;
 args["MIDINITIAL"]=midInitial;
 args["BIRTHDAY"]=birthday;
 args["AGE"]=age;
 args["GENDER"]='M';

 $.post("url/goes/here", args)
 .done(function(returnVal){
     var returnObj = $.parseJSON(returnVal);
     //remaining success code goes here
 })
 .fail(function(){
     //put failure code here
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM