使用jquery和jsonp的ASP.NET跨域Web服务调用错误

时间:2021-08-24 12:19:27

my problem is very similar to the one described here.

我的问题与这里描述的问题非常相似。

however, in my case I am getting the 500 Internal Server Error in firebug:

但是,在我的情况下,我在firebug中得到500内部服务器错误:

Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.

对于意外以“/ HelloWorld”结尾的URL,无法识别请求格式。

my asp.net 4.0 web service call is cross-domain, and from going over a dozen other web sites' advice I believe that I have configured everything in order to allow this to happen correctly, but obviously I have not. what am I doing wrong?

我的asp.net 4.0 Web服务调用是跨域的,并且从过去十几个其他网站的建议我相信我已经配置了所有内容以便允许这种情况正确发生,但显然我没有。我究竟做错了什么?

I am using JSONP instead of JSON. the web service works as expected if everything is on the same server. my problem is that the hosting provider for the html page that calls the web service does not allow server-side code, otherwise i'd just put everything in one place and be done with it!

我使用的是JSONP而不是JSON。如果所有内容都在同一台服务器上,则Web服务按预期工作。我的问题是,调用Web服务的html页面的托管服务提供商不允许服务器端代码,否则我只是将所有内容放在一个地方并完成它!

below is my code/markup:

下面是我的代码/标记:

web.config:

web.config中:

<?xml version="1.0"?>
    <configuration>
  <system.webServer>
    <httpErrors errorMode="Detailed"></httpErrors>
    <asp scriptErrorSentToBrowser="true"></asp>
    <modules>
      <add name="ContentTypeHttpModule"
                    type="ContentTypeHttpModule.ContentTypeHttpModule, ContentTypeHttpModule" />
    </modules>
  </system.webServer>
  <system.web>
    <customErrors mode="Off"></customErrors>
    <compilation debug="true" targetFramework="4.0"/>
    <trust level="Full"/>
    <pages clientIDMode="Static"/>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
    <!--<httpModules>
      <add name="ContentTypeHttpModule"
                    type="ContentTypeHttpModule.ContentTypeHttpModule, ContentTypeHttpModule" />
    </httpModules>-->
  </system.web>
</configuration>

html file javascript:

html文件javascript:

function test() {
            $.ajax({
                url: 'http://designonlinelettering.com/RenderImage.asmx/HelloWorld',
                data: {},
                contentType: "application/json; charset=utf-8",
                dataType: "jsonp",
                success: function (result) {
                    var data = result.d;
                    alert(data);
                },
                error: function (e) {
                    alert('error: ' + e.d);
                }
            });
        }

web service code:

网络服务代码:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}

the ContentTypeHttpModule code was taken from this very informative blog, from which I got most of my direction.

ContentTypeHttpModule代码取自这个信息丰富的博客,我从中获得了大部分指导。

thanks for any help...

谢谢你的帮助...

1 个解决方案

#1


1  

As mentioned, if you view http://designonlinelettering.com/RenderImage.asmx/HelloWorld in a browser you will see the error. adding a ?callback=x doesn't help. It will only return JSON for a POST request with the data/response types set properly.

如上所述,如果您在浏览器中查看http://designonlinelettering.com/RenderImage.asmx/HelloWorld,您将看到错误。添加?callback = x没有帮助。它只会为正确设置数据/响应类型的POST请求返回JSON。

ASMX is problematic and won't return JSON for GET requests... your best bet is to use an ASHX, and Response.Render the JSON (I would recommend using JSON.Net for the encoder).

ASMX是有问题的,不会为GET请求返回JSON ...最好的办法是使用ASHX,而使用JSON的Response.Render(我建议使用JSON.Net作为编码器)。

...
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
...
  var ret = JsonConvert.SerializeObject(
    objectToReturn
    ,new IsoDateTimeConverter()
    ,new DataTableConverter()
    ,new DataSetConverter()
  );
  //only need the datatable, and dataset converters if you're returning those types

  //jsonp should have a callback on the querystring, in your jquery
  // request append  "pathto.ashx?callback=?"
  context.Response.ContentType = "application/javascript";
  context.Response.Write(string.format(
    "{0}({1});"
    ,context.Request.QueryString["callback"]
    ,ret
  ));
...

#1


1  

As mentioned, if you view http://designonlinelettering.com/RenderImage.asmx/HelloWorld in a browser you will see the error. adding a ?callback=x doesn't help. It will only return JSON for a POST request with the data/response types set properly.

如上所述,如果您在浏览器中查看http://designonlinelettering.com/RenderImage.asmx/HelloWorld,您将看到错误。添加?callback = x没有帮助。它只会为正确设置数据/响应类型的POST请求返回JSON。

ASMX is problematic and won't return JSON for GET requests... your best bet is to use an ASHX, and Response.Render the JSON (I would recommend using JSON.Net for the encoder).

ASMX是有问题的,不会为GET请求返回JSON ...最好的办法是使用ASHX,而使用JSON的Response.Render(我建议使用JSON.Net作为编码器)。

...
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
...
  var ret = JsonConvert.SerializeObject(
    objectToReturn
    ,new IsoDateTimeConverter()
    ,new DataTableConverter()
    ,new DataSetConverter()
  );
  //only need the datatable, and dataset converters if you're returning those types

  //jsonp should have a callback on the querystring, in your jquery
  // request append  "pathto.ashx?callback=?"
  context.Response.ContentType = "application/javascript";
  context.Response.Write(string.format(
    "{0}({1});"
    ,context.Request.QueryString["callback"]
    ,ret
  ));
...