php - 接收和处理无连接错误和200 OK header

标签 php ajax json api error-handling

我正在使用自己创建的API(正在与我进行通信),并使用下面的代码。如果一切正常,API将返回json以及 header “200 OK”。但是,如果连接断开,则返回“502 Bad Gateway”作为标题。

到目前为止,“CURLOPT_HEADER”已在我的php脚本中设置为false(下面的se),但是现在我将其设置为true,以便我可以接收 header 并根据 header 采取措施。在这里我需要帮助。

实际上,我有两件事需要帮助(它们相互关联):

  • 如果我的API和源之间的连接正常,并且我在我的php脚本中将CURLOPT_HEADER设置为false(如下所示),则一切正常。但是,如果我将其设置为true,那么实际的 header 也会与ajax请求一起发送回去,并返回错误:“Uncaught SyntaxError:Unexpected token H”(下面的js代码中的第10行)。
  • 如果我的API与来源之间的连接断开,我不知道如何处理该错误,因此该API返回“502 Bad Gateway”。我希望php脚本将这些信息与ajax请求一起发送回去,以便可以在js文件中进行处理。

  • 所以,请在这里帮助我。提前致谢!

    PHP:
    $url = 'http://theurl.com';
    
    $ch = curl_init($url);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    
    $response = curl_exec($ch);
    
    if(!$response) {
        // <--- ??
    }
    
    curl_close($ch);
    
    echo json_encode($response);
    

    JS:
    1.    $.ajax({
    2.        url:'./php/apihandling.php',
    3.        type:'post',
    4.        dataType:'json',
    5.            data:{ 
    6.            data: data
    7.       },
    8.       success: function(data) {
    9.           var content = JSON.parse(data);
    10.          console.log(content);
    11.       },
    12.      error: function (xhr, ajaxOptions, thrownError) {
    13.          console.log(xhr.status);
    14.          console.log(thrownError);
    15.      }
    16.  });
    

    最佳答案

    您可以使用curl_errno检查您的API如何响应您的请求,也可以尝试使用API​​“状态标志”以避免JQuery中的错误

    ...
    // Check if any error occurred
    if(!curl_errno($ch))
    {
     $info = curl_getinfo($ch);
    
     //close the connection
     curl_close($ch);
     $result["API_status"]=0;
     $result["error"]=$info;
     //kill the script and echo the json
     die(json_encode($result));
    } 
    else
    {
    $result["API_status"]=1;
    curl_close($ch);
    $result["response"]=$response;
    echo json_encode($result);
    }
    

    现在让我们尝试一下您的jquery脚本
    $.ajax({
        url: './php/apihandling.php',
        type: 'post',
        dataType: 'json',
        data: {
            data: data
        },
        success: function(data) {
            //no need for this, datatype is json already   
            //var content = JSON.parse(data);
            if (data["API_status"]) {
                alert("wow API is up and healthy");
                //do some stuff with the json
                $.each(data["response"], function(index, value) {
                    //loop this
                });
            } else {
                alert("Oh, no. RIP API");
                console.log(data["error"]);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            console.log(xhr.status);
            console.log(thrownError);
        }
    });
    

    关于php - 接收和处理无连接错误和200 OK header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14957052/

    相关文章:

    java - 如何在android中将HashMap转换为json数组?

    javascript - WordPress - 使用自定义帖子字段中的参数创建动态 onclick 重定向方法

    php - 获取所有非重复的字符串组合(无论顺序如何)

    php - Yii2 : Vote for a Post in AJAX or Pjax

    Javascript 发布数组并在服务器端接收它

    php - 为什么 PHP json_encode 函数将 UTF-8 字符串转换为十六进制实体?

    php - 为什么 PHP 错误会打印两次?

    php - Symfony 2.5 - 从监听器获取表单字段选项

    javascript - JSON 数据和 Ajax 调用的 URL

    PHP随机将大整数减1