0

So my problem is that data returned in success method is undefined...

Here is my jquery code:

  $('document').ready(function(){



      $('#save').click(function() {

      var dataString = $("#stepform").serialize();

      $.ajax({
     url: "<?php echo base_url();?>create/save/<?php echo $row->id; ?>",
     type: 'POST',
     data: dataString,
     success:function(response){

        alert(response.status);

     }
    });//end ajax

    return false;
});//end click

  }); // end document ready

And php code:

$serverResponse["status"] = 'it worked';
echo json_encode($serverResponse);

And all i'm getting from response.status is 'undefined'... I just can't make it work! Any ideas what i'm doing wrong?

-------------EDIT-------------

Finally i have managed to find a solution to invalid json response. If you are using codeigniter you must write exit() after json_encode. Something like this:

exit(json_encode($yourarray));

If you just echo it then it gives a parse error.

1
  • 1
    do you create your php array somewhere Commented May 24, 2013 at 12:14

1 Answer 1

1

jQuery may not be properly guessing that you are returning JSON. Set the dataType: "json" property on the $.ajax argument object, or add

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

to your PHP script before emission, or both.

Sign up to request clarification or add additional context in comments.

7 Comments

@VxsdfsdfAzcxzc in that case you may have either an error/notice/warning in your PHP code or you are printing out something other than just the JSON. Do you get an error in the console?
When i view my request sent with chrome developer tools i see it says "status ok code 200" and in preview tab it says "{"status":"it worked"}" No error in console.
@VxsdfsdfAzcxzc what if you add the error property and give it a callback? Or complete?
Yup error function fires... And after that complete also fires. Also with dataType="json" form data doesn't get passed.
@VxsdfsdfAzcxzc okay it's great that it fires, but what is the error message?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.