对我的Web服务的Ajax查询在我的json中返回xml [英] Ajax query to my webservices is returning xml in my json

查看:119
本文介绍了对我的Web服务的Ajax查询在我的json中返回xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题中提出的确切问题

I am having this exact problem as raised in this question

ASP.NET JSON网络服务始终返回以XML包装的JSON响应

太晚了,我已经在椅子上坐了10个小时了,现在试图使ajax和json正常工作,我所受的挫败感都很大.

It's late and I have been sitting in this chair for 10 hours now trying to get ajax and json to work and all I got was deeply frustrated.

那么没有人知道如何使我的网络服务不返回包装在xml中的json对象吗?如果我只是直接输入dataType:"json",那么我什么也没得到.我必须要做dataType:"jsonp"才能从服务器上取回任何东西.但是一旦我执行jsonp,我就会将json封装在xml中.

So does anyone know how to make my webservice not return my json object wrapped in xml? If I just do a straight dataType: "json" then I get nothing. I have to do dataType: "jsonp" to get anything back from the server at all. But once I do jsonp I get my json wrapped in xml.

请帮助 谢谢 谢丽尔

推荐答案

如果将响应类型设置为json

If you set the response type to json jQuery is then checking the response to see if it's valid JSON (and since it's XML, that's not the case)...when it's not valid, it silently fails since jQuery 1.4+.

提出请求时,有3个重要的地方, :

There are 3 important bits when making your request, by default it needs to be a POST and you need to set the contentType to "application/json; charset=utf-8" like this:

$.ajax({
  url: 'MyService.asmx/Method',
  type: 'POST',
  data: myData,
  dataType: 'json',
  contentType: "application/json; charset=utf-8",
  success: function(data) {
    //do something with data
  }
});

然后在服务器端确保您具有 ScriptService属性,下面是一个非常最小布局的示例:

Then on the server-side make sure you have the ScriptService attribute set, here's an example very minimal layout:

[WebService] //here by default when you create the service
[ScriptService]
public class MyService : System.Web.Services.WebService 
{
  [WebMethod]
  public MyObject MyMethod(int id) 
  {
    return new MyObject();
  }
}

这篇关于对我的Web服务的Ajax查询在我的json中返回xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆