parsererror请求的JSON解析失败| Shopify JSONP跨域

时间:2019-09-13 11:26:19

标签: php jquery ajax shopify jsonp

我已经使用scriptTag Api将脚本嵌入到店面中,然后我从该脚本向我的服务器发送了一个ajax请求,并且正在发送json数据,但错误提示parsererror(请求的JSON解析失败)。每次都会发生此错误,我在https://jsonlint.com/处验证了我的json并显示为“有效JSON”。

这是shopify的ajax代码

$.ajax({
    url: url,
    type: "GET",
    dataType: 'jsonp',
     contentType: "application/json",
     headers: {
         "Access-Control-Allow-Origin": "*",
         "Access-Control-Allow-Headers": "origin, content-type, accept"
     },
    data: {
        'product_id':productId
     },
     success: function(data) //never comes here
     {
         console.log(data);
     },
     error: function (jqXHR, exception) {
          var msg = '';
          console.log(jqXHR);
          if (jqXHR.status === 0) {
              msg = 'Not connect.\n Verify Network.';
          } else if (jqXHR.status == 404) {
              msg = 'Requested page not found. [404]';
          } else if (jqXHR.status == 500) {
              msg = 'Internal Server Error [500].';
          } else if (exception === 'parsererror') {
              msg = 'Requested JSON parse failed.';
          } else if (exception === 'timeout') {
              msg = 'Time out error.';
          } else if (exception === 'abort') {
              msg = 'Ajax request aborted.';
          } else {
              msg = 'Uncaught Error.\n' + jqXHR.responseText;
          }
          console.log(msg);
     },
});

这是我服务器上的代码

$responseArray = array(
      'htmldata' => $data
);
header("Cache-Control: no-transform,public,max-age=300,s-maxage=900");
header('Content-Type: application/json');
$jsonResponse = json_encode($responseArray);
echo $jsonResponse;

0 个答案:

没有答案