Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using the following code i am trying to send JSON data via javascript to a PHP script

Watching "isRunning" while stepping through the code returns true, ie suggests AJAX isn't running, however once the code progresses to the next portion of AJAX code "isRunning" changes to false;

I have tried to trap the error on the second portion - which returns "Unexpected error"

Does anyone have an suggestions as to what i am doing wrong, or how I can trap a more informative error response ?

var isRunning = true;
$.ajax({
    url: url,
    success: function(resp) {
        isRunning = false;
    }
});

jsonString = JSON.stringify(row);

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: 'email_rma.php',
    data: {
        json: jsonString
    },
    success: function(data) {
        console.log('The answer from the PHP file: ' + data);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        if (jqXHR.status == 500) {
            console.log('Internal error: ' + jqXHR.responseText);
        } else {
            console.log('Unexpected error.');
        }
    }
});

Thank you

Thanks to everyone or their suggestions, I think the issue lies with the JSON.stringify conversion, also i noticed i didn't include the PHP side, which i have kept to a minimum basically

$data = json_decode($_POST['json']);
echo "Data:-".$data."<BR>"; 

Is it possible that the problem lies on the PHP side ?

share|improve this question
    
First thing you're doing wrong - expecting the isRunning variable to change right away, it's asynchronous, meaning it runs in parallel while the code you are stepping through is happening. It might change at any later point. –  JNF Oct 26 '14 at 8:08
    
Regarding second half - try to log response text & status for codes other than 500 as well... might help in identifying the error. –  JNF Oct 26 '14 at 8:11
    
Thanks to everyone or their suggestions, I think the issue lies with the JSON.stringify conversion, also i noticed i didn't include teh PHP side, which i have kept to a minimum basically $data = json_decode($_POST['json']); echo "Data:-".$data."<BR>"; Is it possible that the problem lies on the PHP side ? –  rams59 Oct 26 '14 at 14:24
    
Absolutely possible. What happens when you browse to email_rma.php in your browser? Are you positive the json_decode is working? What version of PHP are you running? In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error. –  JNF Oct 26 '14 at 18:20

3 Answers 3

As discussed many times before ajax is asynchronous which means it runs while you are requesting the isRunning variable. If you want to know if this call succeed use a network console like most modern browsers have. This will show you if the call will be made and the server response.

For the second part, the code looks to be correct. However if you run the code from your browser you only see the client side. I suggest you use the network console here as well. A small error in the url may already get your code to output Unexpected error. Either way, you need more information like server response, error codes etc..

share|improve this answer

Have you tried logging jqXHR.responseText with the unexpected error too? Or put a breakpoint on that line and examine the variables in the debugger.

If that doesn't help you could try debugging the php side and seeing how the code executes there and what goes wrong.

Also remember that ajax is asyncronous, so your isRunning could change halfway down the page, or when it's all done.

edit: If you don't have a debugger for php it would definitely save you a ton of headaches in the long run to get one setup. I'd recommend netbeans, since it has a robust toolset for many languages (including both javascript and php). You'd also need to configure xdebug on your server to allow remote debugging.

share|improve this answer

To handle the errors, use the error setting as you're doing, but PHP can return the JSON data too, so you can handle more errors from PHP.

$.ajax({
type: 'POST',
dataType: 'json',
url: 'email_rma.php',
data: {
    json: jsonString
},
success: function(data) {
    if(data.errorCode) {
        console.log('The answer from the PHP file: Error #' + data.errorCode + ' - ' + data.errorException);
    } else {
        console.log('The answer from the PHP file: ' + data.responseText);
    }
},
error: function(jqXHR, textStatus, errorThrown) {
    if (jqXHR.status == 500) {
        console.log('Internal error: ' + jqXHR.responseText);
    } else {
        console.log('Unexpected error.');
    }
}
});

And in your PHP file, you can handle the errors, returning this if the things worked fine:

        $response = array(
            "responseText" => "AJAX is working OK.",
            "errorCode" => 0,
            "errorException" => ""
        );

This if you have to return an error, like MySQL error or something like that:

        $response = array(
            "responseText" => "",
            "errorCode" => 1,
            "errorException" => "Your error message here."
        );

And finally sends the JSON data back to the AJAX:

echo json_encode($response);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.