CORS,ASP.NET Web API,Kendo UI

问题描述 投票:-2回答:1

我有以下情况:

  • 一个ASP.NET Web API项目(托管在本地主机:8081),该项目调用我的BLL来实现业务逻辑
  • 将构成我的Web层的ASP.NET MVC4项目(托管在localhost:8080)
  • 未来计划为移动设备添加项目等
  • 我按照步骤设置了CORS结构,以允许this article中定义的跨域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}
  • [当我使用简单的ajax POST调用从Web应用程序(在本地主机上:8080)执行相同的操作时,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"}
    <<
  • 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调用时设置不起作用

我有以下情况:一个ASP.NET Web API项目(托管在localhost:8081),该项目调用我的BLL来实现业务逻辑一个ASP.NET MVC4项目(托管在localhost:8080),该项目将形成...] >

您正在发布的方法采用数据字符串吗?您是否尝试过仅将javascript对象用作数据?
此外,您首先看到的选项请求是因为您正在进行跨域请求。它发送选项“飞行前”检查以获取Access-Control-Allow-Origin,以查看是否允许发送POST。如果您的localhost:8080不在Access-Control-Allow-Origin中,则它甚至不会发送POST。Anglin在使用kendo启用跨域调用方面做得很好。他使用odata,但某些原则适用。http://www.kendoui.com/blogs/teamblog/posts/11-08-23/cross-domain_queries_to_odata_services_with_jquery.aspx
asp.net asp.net-web-api kendo-ui
1个回答
0
投票
此外,您首先看到的选项请求是因为您正在进行跨域请求。它发送选项“飞行前”检查以获取Access-Control-Allow-Origin,以查看是否允许发送POST。如果您的localhost:8080不在Access-Control-Allow-Origin中,则它甚至不会发送POST。Anglin在使用kendo启用跨域调用方面做得很好。他使用odata,但某些原则适用。http://www.kendoui.com/blogs/teamblog/posts/11-08-23/cross-domain_queries_to_odata_services_with_jquery.aspx
© www.soinside.com 2019 - 2024. All rights reserved.