简体   繁体   中英

PHP cURL sending HTTP 200 ok with no data to AJAX

In a nutshell my AJAX function POST's user input to a PHP file which uses the POST data in collaboration with cURL to return a variable on success back to ajax.

However ajax always receives a 200 OK HTTP code even if the variable is empty, this causes my script to crash.

Happens 1 out of 5 times. Sucks to be the 5th guy but all you have to do is press the button again and it usually doesn't happen consecutively.

--- QUESTION --- Is there a way to to make cURL check the $var before it POST back to ajax and if the $var is empty do the cURL request again.

--- PHP CODE ---

$c = curl_init($blizz_url . $blizz_key);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($c);
if (curl_error($c))
die(curl_error($c));
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);

curl_close($c);


if (!empty($html)) {
 echo $html;
}

---JS CODE---

function ajaxR() {
//by adding the return you can now appy the wait and done params
return $.post("main.php?ajax=true", {name: $('.nameinp').val(), realm:      $('.realminp').val()},
function (data)
    {
        axisT = data;
    }
);

};

I tried a few things but after reading more on stack and other resources I think its my error handling in php that needs work but I really don't know enough about cURL or programming!

Any Help would be appreciated!!!!

All I needed to do was to add a condition in ajax that on success to check data actually contained data and if it did set axisT (the var I wanted to return) to data.

Otherwise run the ajax post again

function ajaxR() {
//by adding the return you can now appy the wait and done params
return $.post("main.php?ajax=true", {name: $('.nameinp').val(), realm: $('.realminp').val()},
function (data)
    {
    if( data !== "undefined"){
    axisT = data;
    } else {
         ajaxR();
    }
    };
);

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