jquery ajax json parsererror

來源:互聯網
上載者:User

 

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>
/// 無任何參數
/// </summary>
/// <returns></returns>
        [WebMethod]
public string HelloWorld()
        {
return "Hello World";
        }
/// <summary>
/// 傳入參數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
        [WebMethod]
public string Hello(string name)
        {
return string.Format("Hello {0}", name);
        }
/// <summary>
/// 返回泛型列表
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
        [WebMethod]
public List<int> CreateArray(int i)
        {
            List<int> list = new List<int>();
while (i >= 0)
            {
                list.Add(i--);
            }
return list;
        }
/// <summary>
/// 返回複雜類型
/// </summary>
/// <param name="name"></param>
/// <param name="age"></param>
/// <returns></returns>
        [WebMethod]
public Person GetPerson(string name, int age)
        {
return new Person()
            {
                Name = name,
                Age = age
            };
        }
    }
/// <summary>
/// 複雜類型
/// </summary>
public class Person
    {
public string Name { get; set; }
public int Age { get; set; }
    }

2、編寫js調用以上方法

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無標題頁</title>
<style type="text/css">
    input
{
        width:200px;
}
</style>
<script type="text/javascript" src="jquery-1[1].2.6.min.js"></script>
<script type="text/javascript">
    $(function(){ 
/*
            1、WebService請求類型都為Post,WebService的Url為“[WebServiceUrl]/[WebMethod]”
            2、contentType聲明為Json
            3、data要用Json的字串格式傳入
            4、設定了dataType為json後,result就直接為返回的Json對象。
*/
//調用無參數方法
        $("#btnHelloWorld").click(function(){
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"WebService1.asmx/HelloWorld",
                data:"{}",
                dataType:'json',
                success:function(result){                   
                    alert(result.d);
                }
            });
        });       
//傳入1個參數
        $("#btnHello").click(function(){
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"WebService1.asmx/Hello",
                data:"{name:'KiMoGiGi'}",
                dataType:'json',
                success:function(result){                   
                    alert(result.d);
                }
            });
        });
//返回泛型列表
        $("#btnArray").click(function(){
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"WebService1.asmx/CreateArray",
                data:"{i:10}",
                dataType:'json',
                success:function(result){                   
                    alert(result.d.join(" | "));
                }
            });
        });
//返回複雜類型
        $("#btnPerson").click(function(){
            $.ajax({
                type: "POST",
                contentType:"application/json",
                url:"WebService1.asmx/GetPerson",
                data:"{name:'KiMoGiGi',age:26}",
                dataType:'json',
                success:function(result){
var person = result.d;
var showText = [];
for(var p in person){
                        showText.push(p + ":" + person[p]);
                    }
                    alert(showText.join("\r\n"));
                }
            });
        });
    });
</script>
</head>
<body>
<form id="form1" runat="server">
<p>
<input type="button" id="btnHelloWorld" value="HelloWorld" />
</p>
<p>
<input type="button" id="btnHello" value="Hello" />
</p>
<p>
<input type="button" id="btnArray" value="CreateArray" />
</p>
<p>
<input type="button" id="btnPerson" value="GetPerson" />
</p>
</form>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.