How to get PHP AJAX error to appear in my jQuery code?

I have PHP AJAX code that should check some parameters sent by jQuery and return some values. Currently, it is returning jQuery error calls in sequence, and I'm not sure why.

Here is my jQuery code:

$('.vote_up').click(function() { alert ( "test: " + $(this).attr("data-problem_id") ); problem_id = $(this).attr("data-problem_id"); var dataString = 'problem_id='+ problem_id + '&vote=+'; $.ajax({ type: "POST", url: "/problems/vote.php", dataType: "json", data: dataString, success: function(json) { // ? :) alert (json); }, error : function(json) { alert("ajax error, json: " + json); //for (var i = 0, l = json.length; i < l; ++i) //{ // alert (json[i]); //} } }); //Return false to prevent page navigation return false; }); 

and here is the PHP code. Validation errors in PHP occur, but I see no indication that the error that occurs on the php side is the one that causes the jQuery error case.

This is the fragment that is called:

 if ( empty ( $member_id ) || !isset ( $member_id ) ) { error_log ( ".......error validating the problem - no member id"); $error = "not_logged_in"; echo json_encode ($error); } 

But how do I get "not_logged_in" so that it appears in my jQuery JavaScript, so that I know that this is the bit that was returned? And if this is not the case, how can I make this error return to jQuery?

Thanks!

+6
source share
3 answers

Not the $ echo error in the json_encode () method, just just echo $ error. Also, do not use json variable, use variable data. Edited code below:

Php

 if ( empty ( $member_id ) || !isset ( $member_id ) ) { error_log ( ".......error validating the problem - no member id"); $error = "not_logged_in"; echo $error; } 

JQuery

 $('.vote_up').click(function() { alert ( "test: " + $(this).attr("data-problem_id") ); problem_id = $(this).attr("data-problem_id"); var dataString = 'problem_id='+ problem_id + '&vote=+'; $.ajax({ type: "POST", url: "/problems/vote.php", dataType: "json", data: dataString, success: function(data) { // ? :) alert (data); }, error : function(data) { alert("ajax error, json: " + data); //for (var i = 0, l = json.length; i < l; ++i) //{ // alert (json[i]); //} } }); //Return false to prevent page navigation return false; }); 
+6
source

jQuery uses the .success(...) method when the response status is 200 (OK), any other status like 404 or 500 is considered an error, so jQuery will use .error(...) .

+2
source

You should handle all the output returned from the php script in the success handler in javascript. Thus, an unregistered user in php can still (should normally ...) lead to a successful ajax call.

If you consistently get an error handler in your javascript call, your PHP script has not been launched or returns a real error instead of a json object.

According to manual , you have 3 variables available in the error handler, so a simple one will check exactly what the problem is:

 // success success: function(data) { if (data == 'not_logged_in') { // not logged in } else { // data contains some json object } }, // ajax error error: function(jqXHR, textStatus, errorThrown) { console.log(jqXHR); console.log(textStatus); console.log(errorThrown); } // 
+1
source

Source: https://habr.com/ru/post/898707/


All Articles