繁体   English   中英

jQuery&CORS始终在打开的远程服务器上失败

[英]jQuery & CORS always fail on an open remote server

我试图在应该启用CORS的远程服务器上使用jQuery $.ajax()方法模拟POST。

想法是提交带有用户名/密码的表单,并从成功的身份验证中获取服务器提供的cookie。

这是jQuery:

$.ajax({
    type: 'POST',
    crossDomain: true,
    async: false,
    url: this.url,
    data: {
        username: this.username,
        password: this.password
    },
    xhrFields: {
        withCreditentials: true
    },
    success: function(responseData, textStatus, jqXHR) {
        console.info(jqXHR.getAllResponseHeaders());
    },
    error: function(responseData, textStatus, errorThrown) {
        console.warn(errorThrown);
    }
});

这是响应头:

HTTP/1.1 200 OK
Connection: close
Content-Type: text/html
Pragma: no-cache
Cache-control: no-cache,max-age=0,must-revalidate
Content-Encoding: gzip
Vary: Accept-Encoding
Transfer-Encoding: chunked

和请求标头:

Accept: * / *
Accept-Encoding: gzip, deflate, sdch
Accept-Language: fr,en-US;q=0.8,en;q=0.6
Cache-Control: max-age=0
Connection: keep-alive
Host: my remote server (URL is hidden)

问题是我得到一个console.warn告诉我:

Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'URL'

我还尝试了使用可模拟POST的Chrome扩展程序:我将与jQuery中相同的数据放入jQuery中,并且成功完成了所有工作,从调用到提交表单后发送的cookie。

我不明白我的jQuery如何无法加载URL,但无论如何都会给我200 OK 而且我不知道该怎么做!

我在哪里做错了?

看来您需要在api响应标头中添加Access-Control-Allow-Origin: *标头。 您的REST客户端(镶边扩展程序)不遵守浏览器之类的CORS规则,这就是它起作用的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM