符合CORS标准的$ .ajax调用复杂的json

时间:2014-04-30 20:06:03

标签: javascript jquery json cors

我理解CORS是stackoverflow中讨论得很好的主题,但我找不到我的特定情况的答案。我需要发布一个复杂的json(即有多个层次结构,所以我必须将其字符串化并将其作为文本发送)到CORS服务器,服务器配置为接受CORS调用(请检查选项响应)但我保留得到CORS错误,我有什么错误吗?任何想法?谢谢

$.ajax({
        url: '//example.com/input',
        type: 'post',
        crossDomain: true,
        dataType: "text",
        contentType: "application/json",
        data: JSON.stringify({
          outer: [
            {
              inner: {
                data: "somedata"
              }
            }
          ],
        })
      });

OPTION

Access-Control-Allow-Headers →Content-Type, If-Match
Access-Control-Allow-Max-Age →21600
Access-Control-Allow-Methods →OPTIONS, GET, POST, HEAD
Access-Control-Allow-Origin →*
Allow →OPTIONS, GET, POST, HEAD
Connection →keep-alive
Content-Length →0
Content-Type →text/html; charset=utf-8
Date →Wed, 30 Apr 2014 20:04:45 GMT

我得到的错误是:

 cannot load exampe.com/input. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'another.com'; is therefore not allowed access.

1 个答案:

答案 0 :(得分:3)

您必须在对基础POST请求的响应中返回Access-Control-Allow-Origin标头(而不仅仅是预检/ OPTIONS)。根据你问题的内容,我只能假设你没有这样做。