0

I have an error about Jquery AJAX post success. After the success function I added code for looking for errors:

...     
else if (exception === 'parsererror') {
  alert('Requested JSON parse failed.');
...

on my PHP code:

echo json_encode(array('msg' => $msg, 'myClass' => $class));

returns back after my function. I checked in Firebug and it returns like,
Response like {"msg":"Kayd\u0131n\u0131z Ba\u015far\u0131l\u0131","myClass":"alert alert-success"}
HTML like {"msg":"Kayd\u0131n\u0131z Ba\u015far\u0131l\u0131","myClass":"alert alert-success"}

There is no JSON tab on firebug and on my AJAX code if I remove the dataType: "json", (it's removed in the code below), my success function is working but I can't get the response.msg or response.myClass, if I don't comment it, success function is not working, here is the Javascript code which invokes the AJAX request:

$.ajax({
  type: "POST",
  url: "index.php?page=addItem&action=addItem&edit=true", 
  data: dataString,
  success: function(response) {
        $("#message").html(response.msg);
        $("#message").addClass(response.myClass);
        $("#itemForm").fadeOut("slow");
        window.setTimeout('location.href = "index.php?page=addItem"', 1000); 
    }   
 });
hakre
  • 193,403
  • 52
  • 435
  • 836
Mesut
  • 11
  • 1
  • 4
  • 1
    And what is your concrete question? – hakre Oct 24 '13 at 09:50
  • 2
    well the JSON string that you have there parses fine so all that i can think is that you have some other data coming back along with the JSON string that is causing the parse to fail if you console.log the response variable what exactly do you get? – Matt Indeedhat Holmes Oct 24 '13 at 09:54
  • 1
    I think you need uncomment "datatype: JSON", and set contentType as "Application/JSON" at server side means in PHP. – Rahul P Oct 24 '13 at 09:58
  • as a note i did once have some issues with an api where if i used dataType:"json" it didnt work but using JSON.parse(response); worked fine, i never did find out what the problem was but i suspect that K Kiran might be on to something there – Matt Indeedhat Holmes Oct 24 '13 at 10:01
  • Please read [*How to get useful error messages in PHP?*](http://stackoverflow.com/q/845021/367456) and take care you log all errors, warnings and notices to a file. You also can log the whole response (albeit inspecting in Firebug first is the way to go). Most likely some errors/warnings are standing in your way you should kill first. Also understand why the JSON got broken, as you already found out `json_encode` works just fine so it can not be the cause of the issue. – hakre Oct 24 '13 at 10:02
  • And the documentation for `datatype: JSON` is here: http://api.jquery.com/jQuery.ajax/ - It also explains if HTTP response headers are needed or not (depending on configuration), so there is no need to guess about that actually. The issue is that the response is not valid JSON from the description given in the question. That is normally added output before or after the JSON string, due to some echoes/prints or some warnings with having display_error on (two common cases). But in any case you should find out what is happening because it is a sign of an underlying problem you'd like to fix, too. – hakre Oct 24 '13 at 10:06
  • here the point i need. i want get response.msg and response.myClass, it was working actually, i was able to show message to user, but now it is not working. i dont know what is wrong. when i uncomment dataType: "json" success function is not working, when i comment it it si working but only the $("#itemForm").fadeOut("slow"); window.setTimeout('location.href = "index.php?page=addItem"', 1000); is working – Mesut Oct 24 '13 at 10:59

0 Answers0