Note that there are some explanatory texts on larger screens.

plurals
  1. POtrouble with json response from php in jQuery.ajax call
    primarykey
    data
    text
    <p>I am using $.ajax to get a JSON response from a php script. if i log the data variable from the $.ajax success function it outputs a properly formatted JSON object, however when I try to access properties of the data var it's undefined. here is the php the is being sent back:</p> <pre><code>echo json_encode(array("status"=&gt;true, "success"=&gt;"Login Success", "message"=&gt;"You have been logged in successfully.")); </code></pre> <p>and here is my ajax call:</p> <pre><code>$.ajax({ type: "POST", data: { login: true, username: $('#login-username').val(), password: $('#login-password').val() }, async: false, url: "./php/client-login.php", success: function (data) { console.log(data.status); if (data.status) { console.log(data.success); displayModal(data.success, data.message, "./js/login-modal-code.js"); } else if (!data.status) { displayModal(data.error, data.message, "./js/login-modal-code.js"); } }, error: function (jqXHR, status, responseText) { console.log(status); } }); </code></pre> <p>if i add the <code>dataType: "json"</code> option to the $.ajax call I get a parse error and if i try to do <code>$.parseJSON(data);</code> to access the data in the data var I get and unexpected token error. I'm not really sure what I'm doing wrong, I've used this setup before and it always has worked before but for some reason it isn't now. anyone see where i've gone wrong?</p> <p>EDIT: forgot to mention here is the response from the php script: </p> <p><code>{"status":true,"success":"Login Success","message":"You have been logged in successfully."}</code></p> <p>EDIT 2: Here is a screen of my console. the top .length call is the json that was logged from <code>console.log(data)</code> and the bottom one is from the response in chrome dev tools network tab for the response from the php script. they line up perfectly yet the second is showing a length of 93, how can i fix this? <img src="https://i.stack.imgur.com/5nHMB.png" alt="Console Image"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload