tags:

views:

4153

answers:

3

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?

Can someone help please I am stuck

<?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

+3  A: 

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

RaYell
I just tried that but it didn't make a different
jasondavis
can you check what type is your data. `alert(typeof data);` in success callback should do it.
RaYell
it says "object"
jasondavis
If you are using Firefox then install Firebug and in your success callback assign data to some global variable. `myglob = data;` Then use DOM tab to find that value and you will see exactly what's inside of it. That should probably give you a clue.
RaYell
+1  A: 

Your PHP array is defined as:

$arr = array ('resonse'=>'error','comment'=>'test comment here');

Notice the mispelling "resonse". Also, as RaYell has mentioned, you have to use data instead of json in your success function because its parameter is currently data.

Try editing your PHP file to change the spelling form resonse to response. It should work then.

John Rasch
thanks that was the problem, I caught it in 1 area of the script but i missed that part
jasondavis
+3  A: 

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:
{"resonse":"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>
Nathan Kleyn
thanks that worked
jasondavis