【问题标题】:Resource interpreted as Script but transferred with MIME type text/xml资源解释为脚本,但使用 MIME 类型 text/xml 传输
【发布时间】:2012-06-18 10:45:43
【问题描述】:

在 VS 2010 中,我有两个项目,一个用于 web 服务,另一个用于 asp.net 页面。

我尝试像这样调用 web 服务(从 .js 的 asp.net 页面):

var host =  "localhost:15932";

var url = "http://" + host + "/MyWebService.asmx/GetFeed?callback=?";


$.ajax({
    async: true,
    type: 'Get',
    url: url,
    data:{ CountryId: CountryId, FeedDateString: FeedDateString, Previous: Previous},
     contentType: 'application/json; charset=utf-8',
     //contentType:'text/xml ',
    processData: true,
    dataType: 'jsonp',

    success: function (response) {
        var result= response.d;
    ,
    error: function (request, status, error) {
        alert(request.responseText);
    }
        ,
    failure: function (msg) {
        alert('somethin went wrong' + msg);
    }
});

调用顺利,但从 web 服务返回的结果是 xml,并且 chrome 显示错误消息:“资源解释为脚本,但使用 MIME 类型文本/xml 传输” 当我在浏览器中输入时,我得到一个 xml 流:

“http://localhost:15932/MyWebService.asmx/GetCountryFeed?callback=jsonp1339760946308&CountryId=1&FeedDateString=&Previous=true”

我的webservice方法的代码:

    [WebMethod(Description = "Get feed")]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet=true)]
    public List<Feed> GetFeed(int CountryId, string FeedDateString, bool Previous)
    {

        return (new UserFeedManager()).GetFeed(CountryId, FeeDate, Previous);
    }

要更改什么以使 response.d 变为 json 格式?

【问题讨论】:

    标签: ajax web-services


    【解决方案1】:

    您需要覆盖 Web 服务响应。

    1. 将 GetFeed 的返回类型设置为 void。
    2. 序列化列表。
    3. 返回 json: HttpContext.Current.Response.ContentType = "文本/javascript"; HttpContext.Current.Response.Write(json) HttpContext.Current.Response.End();

    您可能想要创建一个类来处理您的响应并传入您的函数,这些函数返回带有 Func 函数或 Func 参数的 json。

    【讨论】:

      猜你喜欢
      • 2012-11-23
      • 2011-07-18
      • 2014-07-12
      • 2014-12-01
      • 2014-07-15
      • 1970-01-01
      • 2013-08-03
      • 2020-05-06
      • 2011-12-04
      相关资源
      最近更新 更多