简体   繁体   English

PHP cURL将没有数据的HTTP 200 OK发送到AJAX

[英]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. 简而言之,我的AJAX函数将POST的用户输入输入到PHP文件中,该文件使用POST数据与cURL协作,将成功时的变量返回给ajax。

However ajax always receives a 200 OK HTTP code even if the variable is empty, this causes my script to crash. 但是,即使变量为空,ajax也会始终收到200 OK HTTP代码,这将导致我的脚本崩溃。

Happens 1 out of 5 times. 发生5次中的1次。 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. ---问题---有没有办法使cURL在回传到ajax之前检查$ var,如果$ var为空,请再次执行cURL请求。

--- PHP CODE --- -PHP代码-

$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--- --- JS代码---

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! 我尝试了一些尝试,但是在阅读了更多堆栈和其他资源后,我认为这是我在php中需要工作的错误处理方式,但我对cURL或编程的了解还不够多!

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. 我需要做的就是在ajax中添加一个条件,该条件可以成功检查数据中实际包含的数据,以及是否确实将axisT(我想返回的var)设置为数据。

Otherwise run the ajax post again 否则,再次运行ajax发布

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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