Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get JSON response into variable from a jquery script

Tags:

I am having trouble with my jQuery script below, this is a basic stripped down version and even it will not work, I have the php file that the jQuery script makes a call to, I have it set to encode and show a JSON response

Then in the jQuery script it should read the value and respond to it but It is not getting the response.

Is json.response the wrong way to call a variable in the JSON string that is name response?

<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

// set to retunr response=error
$arr = array ('resonse'=>'error','comment'=>'test comment here');
echo json_encode($arr);
?>

//the script above returns this:
{"response":"error","comment":"test comment here"}

<script type="text/javascript">
$.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    dataType: "json",
    success: function (data) {
        if (json.response == 'captcha') {
            alert('captcha');
        } else if (json.response == 'error') {
            alert('sorry there was an error');
        } else if (json.response == 'success') {
            alert('sucess');

        };
    }

})
</script>

UPDATE:

I have changed

json.response  

into

data.response  

But this did not make it ork either

like image 255
JasonDavis Avatar asked Aug 11 '09 17:08

JasonDavis


People also ask

How can I get data from JSON to variable?

This will do it: var json = (function () { var json = null; $. ajax({ 'async': false, 'global': false, 'url': my_url, 'dataType': "json", 'success': function (data) { json = data; } }); return json; })(); The main issue being that $.

What is getJSON in jQuery?

jQuery getJSON() Method The getJSON() method is used to get JSON data using an AJAX HTTP GET request.

How do I get JSON response?

json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.

What does getJSON return?

getJSON( url, [data], [callback] ) method loads JSON data from the server using a GET HTTP request. The method returns XMLHttpRequest object.


2 Answers

Here's the script, rewritten to use the suggestions above and a change to your no-cache method.

<?php
// Simpler way of making sure all no-cache headers get sent
// and understood by all browsers, including IE.
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));

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

// set to return response=error
$arr = array ('response'=>'error','comment'=>'test comment here');
echo json_encode($arr);
?>

//the script above returns this:
{"response":"error","comment":"test comment here"}

<script type="text/javascript">
$.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    dataType: "json",
    success: function (data) {
        if (data.response == 'captcha') {
            alert('captcha');
        } else if (data.response == 'success') {
            alert('success');
        } else {
            alert('sorry there was an error');
        }
    }

}); // Semi-colons after all declarations, IE is picky on these things.
</script>

The main issue here was that you had a typo in the JSON you were returning ("resonse" instead of "response". This meant that you were looking for the wrong property in the JavaScript code. One way of catching these problems in the future is to console.log the value of data and make sure the property you are looking for is there.

Learning how to use the Chrome debugger tools (or similar tools in Firefox/Safari/Opera/etc.) will also be invaluable.

like image 99
Nathan Kleyn Avatar answered Oct 07 '22 01:10

Nathan Kleyn


You should use data.response in your JS instead of json.response.

like image 44
RaYell Avatar answered Oct 07 '22 01:10

RaYell