如何为Ajax GET请求修复'{"readyState":0,"status":0,"statusText":"error"}' [英] How to fix ' {"readyState":0,"status":0,"statusText":"error"}' for Ajax GET request

查看:1341
本文介绍了如何为Ajax GET请求修复'{"readyState":0,"status":0,"statusText":"error"}'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建API以便将数据插入我的网站(可能在不同的域中),但是无论我如何设置响应的格式,我的ajax调用都无法检索到任何数据.

I'm trying build an API to insert data into my website (necessarily in different domains), but no matter how I format the response, my ajax calls fail to retrieve any data.

我尝试修改响应的标头,尤其是包含Access-Control-Allow-Origin: *以避免CORS问题.我也尝试将响应格式化为JSONP,但无济于事.

I have tried modifying the headers of the response, in particular to include Access-Control-Allow-Origin: * to avoid CORS problems. I also tried formatting the response as a JSONP, to no avail.

这是我无法工作的最少代码.

Here's a minimal piece of code that I can't get to work.

<!DOCTYPE html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script type="text/javascript">

$.ajax({

    url : 'http://157.230.17.221/test/test',
    type : 'GET',
    dataType:'json',
    success : function(data) {              
        alert('Data: '+data);
    },
    error : function(request,error)
    {
        alert("Request: "+JSON.stringify(request)+JSON.stringify(error));
    }
});
</script>

</head>

</html>

该呼叫转到带有{"readyState":0,"status":0,"statusText":"error"}响应的错误警报.我很乐意得到有关如何设置响应格式的任何建议,对该代码的任何有效修改或可以完成此工作的ajax的替代方案.目的是将响应注入到html中.

The call goes to the error alert with a {"readyState":0,"status":0,"statusText":"error"} response. I'd be happy to get any suggestions on how to format the response, any modifications to this code that work or any alternatives to ajax that would do the job. The goal is to inject the response into the html.

推荐答案

您的Access-Control-Allow-Origin标头很好:

HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Mon, 15 Jul 2019 22:34:45 GMT
Content-Type: application/json
Content-Length: 26
Connection: keep-alive
Vary: Accept-Encoding
Content-Security-Policy: frame-ancestors 'none'
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type, Cookie, Set-Cookie, Authorization
Access-Control-Allow-Credentials: true
Cache-Control: public, max-age=10

{"name":"john","age":"33"}

我怀疑您要加载此页面的页面是HTTPS,但是您的API服务仅支持HTTP.我可以通过JSFiddle重现该问题. (注释中的链接,因为Stack Overflow不允许我将其放在此处.)

I suspect that the page you're loading this on is HTTPS, but your API service only supports HTTP. I can reproduce the issue via JSFiddle. (Link in the comments, because Stack Overflow won't let me put it here.)

当您的页面使用HTTPS时,任何XHR或Fetch请求也必须使用HTTPS.您的服务仅响应HTTP,这会导致发生此错误.

When you have a page that uses HTTPS, any XHR or Fetch requests must also use HTTPS. Your service only responds to HTTP, which causes this error to occur.

要修复此问题,请同时在您的API服务上启用HTTPS.

To fix it, enable HTTPS on your API service as well.

这篇关于如何为Ajax GET请求修复'{"readyState":0,"status":0,"statusText":"error"}'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆