1

I cannot seem to understand why I cannot access an array I get send back by PHP.

I send this back when AJAX makes a call: $response['success'] = true;

I do this by echo json_encode($response); in my PHP file.

Now I want to access it in Javascript, but response.success doesnt work, it logs 'undefined' in the console. Now I check the response, and that is this: {"success":false}

But if I check for if(response.success) it always sends back false, because response.success is undefined. Does anyone know what I mean and what is causing this issue?

This is my AJAX call:

$$.ajax({
    type: 'POST',
    url: url + "applogin.php",
    crossDomain: true,
    data: {
        username: e,
        password: p
    },
    //dataType: 'json', 
    success: function(response) {
        console.log(response);

        if (response["success"]) {

            window.localStorage["username"] = e;
            window.localStorage["password"] = md5(p);

            mainView.router.loadPage('beurslist.html');
        } else {

            console.log("Your login failed");
        }
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log("Error- Status: " + textStatus + " jqXHR Status: " + jqXHR.status + " jqXHR Response Text:" + jqXHR.responseText)
    },

}); 
5
  • Show the full code of AJAX request processing Commented May 17, 2015 at 17:12
  • see above! i edited the question with the ajax call Commented May 17, 2015 at 17:17
  • response.success should be the way. Are your sure you are returning the correct JSON? What does console.log(response) output? Commented May 17, 2015 at 17:28
  • You have to set dataType option to json. Otherwise the response will be treated as html (by default) Commented May 17, 2015 at 17:34
  • @hindmost - From the docs: dataType (default: Intelligent Guess (xml, json, script, or html)) Commented May 17, 2015 at 18:06

2 Answers 2

2

The answer by @SZenC is fine, there's a point though about best practices:

The reason jQuery didn't recognize the response as JSON is because the server probably didn't send a Content-type header, so just add to your server-side code a call to -

header('Content-type: text/json');

You won't need the explicit call to JSON.parse in each API call, you'll get better integration with test tools, and your app will be better self-documented for other folks who use/will-use this code (including yourself should your code need some future maintenance).

In any case it is advised also to document the expected input on the client side by either explicitly specifying dataType as json or by making the API call using a shortcut method such as $.getJSON().

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

Comments

1

Your ajax-call will return a string, you need to decode it with JSON.parse. You'd get something like this then.

$$.ajax({
  type: 'POST',
  url: url + "applogin.php",
  crossDomain: true,
  data: {
    username: e,
    password: p
  },
  //dataType: 'json', 
  success: function(r) {
    var response=JSON.parse(r)
    console.log(response);
    if (response["success"]) {
      window.localStorage["username"] = e;
      window.localStorage["password"] = md5(p);
      mainView.router.loadPage('beurslist.html');
    } else {
      console.log("Your login failed");
    }
  },
  error: function(jqXHR, textStatus, errorThrown) {
    console.log("Error- Status: " + textStatus + " jqXHR Status: " + jqXHR.status + " jqXHR Response Text:" + jqXHR.responseText)
  }
});

2 Comments

Thank you! This is indeed the way, i feel stupid for overseeing such a simple mistake.
@HuubS, it happens to the best of us

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.