-2

我有以下情况:

  • 一个 ASP.NET Web API 项目(托管在 localhost:8081),它调用我的 BLL 来实现业务逻辑
  • 一个 ASP.NET MVC4 项目(托管在 localhost:8080),它将形成我的 Web 层
  • 未来计划为移动等添加项目
  • 我按照本文中定义的步骤设置 CORS 结构以允许跨域 ajax 请求

我有一个 API 服务可以从数据库中返回“somelist”。

  • 当我通过 Fiddler 做一个简单的请求时,我得到了服务器的正确响应

要求:

 
    POST http://localhost:8081/api/orgs/getsomelist HTTP/1.1
    User-Agent: Fiddler
    Content-type: application/json
    Host: localhost:8081
    Content-Length: 158

    {"token": "sometoken","kValues": { "userId": "1.0" }}

回复:


    HTTP/1.1 200 OK
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Type: application/json; charset=utf-8
    Expires: -1
    Server: Microsoft-IIS/8.0
    X-AspNet-Version: 4.0.30319
    X-SourceFiles: =?UTF-8?B?WjpcRG9jdW1lbnRzXGNvZGVcSnNyZWVcSnNyZWVcSnNyZWUuQXBpXGFwaVxvcmdzXGdldG15b3Jncw==?=
    X-Powered-By: ASP.NET
    Date: Thu, 13 Dec 2012 22:15:34 GMT
    Content-Length: 315

    {successful JSON response}
  • 当我通过 Web 应用程序(在 localhost:8080 上)使用简单的 ajax POST 调用执行相同操作时,Fiddler 显示 2 个请求,1 个返回 200 成功,另一个返回 500 个内部服务器

    Code:
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8;",
        crossDomain: true,
        url: "@urlToConnect",
        withCredentials: true,
        dataType: "json",
        data: JSON.stringify({token: "sometoken",kValues: { userId: "1.0" }})
    });

第一个请求:由于某种原因,它显示 OPTIONS 调用而不是 POST


    OPTIONS http://localhost:8081/api/orgs/getsomelist HTTP/1.1
    Host: localhost:8081
    Connection: keep-alive
    Access-Control-Request-Method: POST
    Origin: http://localhost:8080
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
    Access-Control-Request-Headers: origin, content-type, accept
    Accept: */*
    Referer: http://localhost:8080/
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: en-US,en;q=0.8
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

第一个回应:


    HTTP/1.1 200 OK
    Cache-Control: no-cache
    Pragma: no-cache
    Expires: -1
    Server: Microsoft-IIS/8.0
    Access-Control-Allow-Origin: http://localhost:8080
    Access-Control-Allow-Methods: POST
    Access-Control-Allow-Headers: origin, content-type, accept
    X-AspNet-Version: 4.0.30319
    X-SourceFiles: =?UTF-8?B?WjpcRG9jdW1lbnRzXGNvZGVcSnNyZWVcSnNyZWVcSnNyZWUuQXBpXGFwaVxvcmdzXGdldG15b3Jncw==?=
    X-Powered-By: ASP.NET
    Date: Thu, 13 Dec 2012 22:15:16 GMT
    Content-Length: 0

第二个要求:


    POST http://localhost:8081/api/orgs/getsomelist HTTP/1.1
    Host: localhost:8081
    Connection: keep-alive
    Content-Length: 127
    Origin: http://localhost:8080
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
    Content-Type: application/json; charset=UTF-8;
    Accept: application/json, text/javascript, */*; q=0.01
    Referer: http://localhost:8080/
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: en-US,en;q=0.8
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

    {"token": "sometoken","kValues": { "userId": "1.0" }}

第二个响应:我的 Web API 方法无法正确获取 POSTed JSON 数据。在上面的示例 1 中直接在 Fiddler 上发布时,我没有得到具有相同数据的异常


    HTTP/1.1 500 Internal Server Error
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Type: application/json; charset=utf-8
    Expires: -1
    Server: Microsoft-IIS/8.0
    Access-Control-Allow-Origin: http://localhost:8080
    X-AspNet-Version: 4.0.30319
    X-SourceFiles: =?UTF-8?B?WjpcRG9jdW1lbnRzXGNvZGVcSnNyZWVcSnNyZWVcSnNyZWUuQXBpXGFwaVxvcmdzXGdldG15b3Jncw==?=
    X-Powered-By: ASP.NET
    Date: Thu, 13 Dec 2012 22:15:16 GMT
    Content-Length: 906

    {"Message":"An error has occurred.","ExceptionMessage":"some .NET exception"}
  • 当我使用 kendoui 数据源做同样的事情时,我得到了同样的 2 个错误,但是我对提琴手的请求显示垃圾数据,而不是我发送的 JSON

Code:
    dataSource: new kendo.data.DataSource({
            transport: {
                read: {
                    type: "POST",
                    contentType: "application/json; charset=utf-8;",
                    crossDomain:true,
                    url: "@urlToConnect",
                    withCredentials: true,
                    dataType: "json",
                    data: JSON.stringify({token: "sometoken",kValues: { userId: "1.0" }})
                }
            },
            schema: {
                data: "data",
                type: "json",
                total: "count"
            }

请求:JSON.stringify 发送奇怪的数据


    POST http://localhost:8081/orgs/somelist HTTP/1.1
    Host: localhost:8081
    Connection: keep-alive
    Content-Length: 705
    Cache-Control: max-age=0
    Origin: http://localhost:8080
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
    Content-Type: application/json; charset=UTF-8;
    Accept: application/json, text/javascript, */*; q=0.01
    Referer: http://localhost:8080/
    Accept-Encoding: gzip,deflate,sdch
    Accept-Language: en-US,en;q=0.8
    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

    0=%7B&1=%22&2=t&3=o&4=k&5=e&6=n&7=%22&8=%3A&9=%22&10=1&11=F&12=V&13=R&14=A&15=I&16=1&17=E&18=b&19=7&20=%2F&21=%2F&22=M&23=7&24=U&25=A&26=Y&27=h&28=q&29=9&30=i&31=R&32=Z&33=U&34=%2F&35=Y&36=j&37=l&38=o&39=0&40=Q&41=5&42=Z&43=g&44=A&45=H&46=8&47=4&48=%2F&49=5&50=b&51=E&52=g&53=Z&54=F&55=M&56=%2B&57=p&58=Q&59=j&60=0&61=x&62=4&63=w&64=Z&65=6&66=W&67=c&68=T&69=M&70=u&71=Q&72=n&73=K&74=3&75=P&76=x&77=e&78=T&79=C&80=%2F&81=0&82=K&83=K&84=W&85=1&86=w&87=o&88=5&89=2&90=%2B&91=d&92=V&93=R&94=v&95=A&96=%3D&97=%3D&98=%22&99=%2C&100=%22&101=k&102=V&103=a&104=l&105=u&106=e&107=s&108=%22&109=%3A&110=%7B&111=%22&112=u&113=s&114=e&115=r&116=I&117=d&118=%22&119=%3A&120=%22&121=1&122=.&123=0&124=%22&125=%7D&126=%7D

我不确定为什么当我直接或从 kendoui 数据源进行 ajax 调用时设置不起作用

4

1 回答 1

0

您发布的方法是否为数据获取字符串?您是否尝试过仅使用 javascript 对象作为数据?

此外,您首先看到的选项请求是因为您正在发出跨域请求。它发送选项“飞行前”检查以获取 Access-Control-Allow-Origin 以查看是否允许发送 POST。如果您的 localhost:8080 不在 Access-Control-Allow-Origin 中,那么它甚至不会发送 POST。Anglin 写了一篇关于使用 kendo 启用跨域调用的精彩文章。他使用 odata,但有些原则适用。 http://www.kendoui.c​​om/blogs/teamblog/posts/11-08-23/cross-domain_queries_to_odata_services_with_jquery.aspx

于 2012-12-14T17:53:45.993 回答