0

I've been trying to troubleshoot this, but can't find a good function that will execute a full stack trace. Here's what I have so far:

Javascript (in page /charts/userbase.php):

$.ajax({
    url: '/data/users.php',
    dataType: 'json',
    error: function() {
      console.log("I'm hitting an error.");
    },
    success: function(jsonData) {
      // do something
    }
});

/data/users.php (should dump JSON)

<?php
  //some processing precedes this
  $data = json_encode($data, JSON_NUMERIC_CHECK);
  $data = str_replace("\/", "/", $data);
  $data = str_replace("\"", "", $data);

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

?>

Visiting 127.0.0.1/data/users.php returns the correct JSON, but somehow I can't get jQuery's $.ajax() to work, and I can't seem to find any good documentation on how to output a stack trace if it fails. Any ideas on what I'm doing wrong?

EDIT: Damn, that was a lot of responses, really fast.

First off, I do post-procession on the JSON string because PHP keeps changing / into \/, which JS's native Date() can't handle, and it keeps adding ", which breaks parts of the code expecting booleans. I have already verified that it is returning the correct output.

Second, changing error to add jsonData prints out undefined, and the error(jqXHR, textStatus, errorThrown) returns an object, with status 200.

Maybe I'm wrong and it's not the ajax call, but I'm way more confused now.

EDIT 2: Running with console.log(arguments), I'm getting a parseError. I'll take a closer look, but I don't see anything wrong with what I have.

Last Edit: Turns out everyone was right on that last line; shouldn't have replaced the ". Still need str_replace("\/", "/", $data) though. I feel pretty dumb it took me a few hours to troubleshoot this, though.

9
  • Does the console.log statement execute? Why not use the error function's parameters: error(jqXHR, textStatus, errorThrown) Commented Feb 22, 2012 at 20:09
  • 3
    You are modifying the json string after using json_encode. Why are you doing this and are you sure that it's still a valid json string after this modification? Commented Feb 22, 2012 at 20:11
  • Need more info about what the $.ajax call does or doesn't do. Commented Feb 22, 2012 at 20:11
  • As @Jody said, you're very likely corrupting the JSON string with your str_replace lines. Commented Feb 22, 2012 at 20:18
  • Remember that the error parameter doesn't just automatically know that generically "something went wrong". It means that a server error was returned. The server can be successfully returning something that your success function doesn't handle, and the error message will still never alert. Commented Feb 22, 2012 at 20:19

2 Answers 2

1

Check out http://api.jquery.com/jQuery.ajax/

What happens if you try

$.ajax({
    url: 'data/users.php',
    dataType: 'json',
    error: function() {
      console.log("I'm hitting an error.");
      console.log(arguements);
    },
    success: function(jsonData) {
      // do something
      console.log(jsonData);
    }
});

You should see console logs stating an error occurred because you are modifying the json string after you encoded it.

EDIT:

You say your JSON is good. Try using this to verify http://jsonlint.com/

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

2 Comments

@Edwin - or console.log(arguments)
Refering someone to the overall documentation shouldn't quite qualify as an answer - this might be better as a comment.
0

can you add this to your ajax:

contentType: "application/json",

Comments

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.