user2466707
user2466707

Reputation: 29

ajax success callback not working on json response

AJAX request was successfully sent to php server and also inserted records on my oracle database. I cannot get the JSON response in success, error, complete callback functions.

I can't also see any logs on console.log

I am using codeigniter and my php server was on model folder and it returns an json formatted string.

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

Here is my ajax code:

$.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");
    }
});

Upvotes: 0

Views: 1288

Answers (2)

Fredmat
Fredmat

Reputation: 958

Set content type in your PHP file:

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

Upvotes: 1

user3621626
user3621626

Reputation:

You might also want to give this a try (using 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
 });

Upvotes: 0

Related Questions