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