简体   繁体   中英

JSON data from javascript to PHP with Jquery AJAX

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 ?

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

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM