【问题标题】:PHP cURL sending HTTP 200 ok with no data to AJAXPHP cURL 向 AJAX 发送没有数据的 HTTP 200 ok
【发布时间】:2016-04-07 01:35:25
【问题描述】:

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

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

发生 5 次中的 1 次。成为第五个人很糟糕,但你所要做的就是再次按下按钮,而且通常不会连续发生。

--- 问题 --- 有没有办法让 cURL 在 $var POST 回 ajax 之前检查它,如果 $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 或编程知之甚少!

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

【问题讨论】:

    标签: php jquery ajax http curl


    【解决方案1】:

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

    否则再次运行ajax post

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 2016-03-09
      • 2014-01-11
      • 1970-01-01
      相关资源
      最近更新 更多