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

Javascript 使用jquery和jsonp的ASP.NET跨域web服务调用错误,javascript,jquery,asp.net,web-services,jsonp,Javascript,Jquery,Asp.net,Web Services,Jsonp,我的问题与描述的问题非常相似 但是,在我的例子中,firebug中出现了500内部服务器错误: 无法识别意外以“/HelloWorld”结尾的URL的请求格式 我的asp.NET4.0Web服务调用是跨域的,从十几个其他网站的建议来看,我相信我已经配置了所有配置,以便正确地实现这一点,但显然我没有。我做错了什么 我使用的是JSONP而不是JSON。如果所有内容都在同一台服务器上,则web服务可以正常工作。我的问题是,调用web服务的html页面的宿主提供程序不允许使用服务器端代码,否则我会将所有

我的问题与描述的问题非常相似

但是,在我的例子中,firebug中出现了500内部服务器错误

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

我的asp.NET4.0Web服务调用是跨域的,从十几个其他网站的建议来看,我相信我已经配置了所有配置,以便正确地实现这一点,但显然我没有。我做错了什么

我使用的是JSONP而不是JSON。如果所有内容都在同一台服务器上,则web服务可以正常工作。我的问题是,调用web服务的html页面的宿主提供程序不允许使用服务器端代码,否则我会将所有内容放在一个地方,然后处理完

下面是我的代码/标记:

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>
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);
                }
            });
        }
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
web服务代码:

<?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>
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);
                }
            });
        }
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
ContentTypeHttpModule代码取自这篇内容丰富的文章,我从中获得了大部分指导

谢谢你的帮助

如前所述,如果在浏览器中查看,您将看到错误。添加?callback=x没有帮助。对于设置了正确的数据/响应类型的POST请求,它将只返回JSON

ASMX有问题,无法为GET请求返回JSON。。。最好使用ASHX和Response.Render来呈现JSON(我建议编码器使用JSON.Net)

。。。
使用Newtonsoft.Json;
使用Newtonsoft.Json.Converters;
使用Newtonsoft.Json.Linq;
...
var ret=JsonConvert.SerializeObject(
反对返回
,新的IsoDateTimeConverter()
,新的DataTableConverter()
,新的DataSetConverter()
);
//如果要返回这些类型,则只需要datatable和dataset转换器
//jsonp应该在jquery中的querystring上有一个回调
//请求追加“pathto.ashx?回调=?”
context.Response.ContentType=“应用程序/javascript”;
context.Response.Write(string.format(
"{0}({1});"
,context.Request.QueryString[“回调”]
,ret
));
...
如前所述,如果在浏览器中查看,您将看到错误。添加?callback=x没有帮助。对于设置了正确的数据/响应类型的POST请求,它将只返回JSON

ASMX有问题,无法为GET请求返回JSON。。。最好使用ASHX和Response.Render来呈现JSON(我建议编码器使用JSON.Net)

。。。
使用Newtonsoft.Json;
使用Newtonsoft.Json.Converters;
使用Newtonsoft.Json.Linq;
...
var ret=JsonConvert.SerializeObject(
反对返回
,新的IsoDateTimeConverter()
,新的DataTableConverter()
,新的DataSetConverter()
);
//如果要返回这些类型,则只需要datatable和dataset转换器
//jsonp应该在jquery中的querystring上有一个回调
//请求追加“pathto.ashx?回调=?”
context.Response.ContentType=“应用程序/javascript”;
context.Response.Write(string.format(
"{0}({1});"
,context.Request.QueryString[“回调”]
,ret
));
...

contentType和$error选项。jsonp请求不需要ajax,因为您得到的错误是服务器端的,与jQuery无关。直接导航到
http://designonlinelettering.com/RenderImage.asmx/HelloWorld
您将看到$.contentType和error选项。jsonp请求不需要ajax,因为您得到的错误是服务器端的,与jQuery无关。直接导航到
http://designonlinelettering.com/RenderImage.asmx/HelloWorld
您将看到。