繁体   English   中英

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

[英]PHP cURL sending HTTP 200 ok with no data to AJAX

简而言之,我的AJAX函数将POST的用户输入输入到PHP文件中,该文件使用POST数据与cURL协作,将成功时的变量返回给ajax。

但是,即使变量为空,ajax也会始终收到200 OK HTTP代码,这将导致我的脚本崩溃。

发生5次中的1次。 很遗憾成为第五名,但是您要做的就是再次按下按钮,通常不会连续发生。

---问题---有没有办法使cURL在回传到ajax之前检查$ var,如果$ var为空,请再次执行cURL请求。

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

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

};

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

任何帮助,将不胜感激!!!!

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

否则,再次运行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