jquery ajax json parsererror

data:"{}", data为空也一定要传"{}";不然返回的是xml格式的。并提示parsererror

1、编写4种WebService方法

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService] //令WebService成功传入Json参数,并以Json形式返回结果
[GenerateScriptType(typeof(Person))] //不是必要,但推荐添加(如果Person里面再嵌套另一个复杂类型,则必要声明)
[ToolboxItem(false)]
public class WebService1 : System.Web.Services.WebService
{
/// <summary><br>/// 无任何参数<br>/// </summary>
/// <returns></returns>
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
/// <summary><br>/// 传入参数<br>/// </summary>
///
/// <returns></returns>
[WebMethod]
public string Hello(string name)
{
return string.Format("Hello {0}", name);
}
/// <summary><br>/// 返回泛型列表<br>/// </summary>
///
/// <returns></returns>
[WebMethod]
public List<int> CreateArray(int i)<br> {<br> List<int> list = new List<int>();<br>while (i &gt;= 0)<br> {<br> list.Add(i--);<br> }<br>return list;<br> }<br>/// <summary><br>/// 返回复杂类型<br>/// </summary><br>/// <param name="name"> <br>/// <param name="age"> <br>/// <returns></returns><br> [WebMethod]<br>public Person GetPerson(string name, int age)<br> {<br>return new Person()<br> {<br> Name = name,<br> Age = age<br> };<br> }<br> }<br>/// <summary><br>/// 复杂类型<br>/// </summary><br>public class Person<br> {<br>public string Name { get; set; }<br>public int Age { get; set; }<br> } <p>2、编写js调用以上方法 </p> <p><br><br><br></p> <br><title>无标题页</title> <br><style type="text/css"><br> input<br>{<br> width:200px;<br>}<br></style> <br><br><br> $(function(){ <br>/*<br> 1、WebService请求类型都为Post,WebService的Url为“[WebServiceUrl]/[WebMethod]”<br> 2、contentType声明为Json<br> 3、data要用Json的字符串格式传入<br> 4、设置了dataType为json后,result就直接为返回的Json对象。<br>*/<br>//调用无参数方法<br> $("#btnHelloWorld").click(function(){<br> $.ajax({<br> type: "POST",<br> contentType:"application/json",<br> url:"WebService1.asmx/HelloWorld",<br><strong>data:"{}",<br></strong> dataType:'json',<br> success:function(result){ <br> alert(result.d);<br> }<br> });<br> }); <br>//传入1个参数<br> $("#btnHello").click(function(){<br> $.ajax({<br> type: "POST",<br> contentType:"application/json",<br> url:"WebService1.asmx/Hello",<br> data:"{name:'KiMoGiGi'}",<br> dataType:'json',<br> success:function(result){ <br> alert(result.d);<br> }<br> });<br> });<br>//返回泛型列表<br> $("#btnArray").click(function(){<br> $.ajax({<br> type: "POST",<br> contentType:"application/json",<br> url:"WebService1.asmx/CreateArray",<br> data:"{i:10}",<br> dataType:'json',<br> success:function(result){ <br> alert(result.d.join(" | "));<br> }<br> });<br> });<br>//返回复杂类型<br> $("#btnPerson").click(function(){<br> $.ajax({<br> type: "POST",<br> contentType:"application/json",<br> url:"WebService1.asmx/GetPerson",<br> data:"{name:'KiMoGiGi',age:26}",<br> dataType:'json',<br> success:function(result){<br>var person = result.d;<br>var showText = [];<br>for(var p in person){<br> showText.push(p + ":" + person[p]);<br> }<br> alert(showText.join("\r\n"));<br> }<br> });<br> });<br> });<br><br><br><br><form id="form1" runat="server"> <br><p><br><input type="button" id="btnHelloWorld" value="HelloWorld"><br></p> <br><p><br><input type="button" id="btnHello" value="Hello"><br></p> <br><p><br><input type="button" id="btnArray" value="CreateArray"><br></p> <br><p><br><input type="button" id="btnPerson" value="GetPerson"><br></p> <br> </form> <br><br></int></int></int>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值