2

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

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

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”

我的网络服务方法的代码:

    [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 格式?

4

1 回答 1

0

您需要覆盖 Web 服务响应。

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

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

于 2015-04-03T15:54:30.953 回答