Jquery Ajax JSON parsererror

Source: Internet
Author: User

 

Data: "{}". If the data is empty, you must pass "{}". Otherwise, the returned data is in XML format. The system prompts parsererror.

1. Write four WebService Methods

[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Scriptservice] // enables WebService to pass in JSON parameters and return results in JSON format
[Generatescripttype (typeof (person)] // not necessary, but it is recommended to add (if another complex type is nested in person, it must be declared)
[Toolboxitem (false)]
Public class webservice1: system. Web. Services. WebService
{
/// <Summary>
/// No parameters
/// </Summary>
/// <Returns> </returns>
[Webmethod]
Public String helloworld ()
{
Return "Hello World ";
}
/// <Summary>
/// Input parameters
/// </Summary>
/// <Param name = "name"> </param>
/// <Returns> </returns>
[Webmethod]
Public String Hello (string name)
{
Return string. Format ("Hello {0}", name );
}
/// <Summary>
/// Return the generic list
/// </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>
/// Return complex types
/// </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>
/// Complex type
/// </Summary>
Public class person
{
Public string name {Get; set ;}
Public int age {Get; set ;}
}

2. Compile js to call the above method

<% @ 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> No title page </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. the WebService request type is post, and the WebService URL is "[webserviceurl]/[webmethod]"
2. contenttype is declared as JSON
3. data must be transmitted in JSON string format.
4. After the ype is set to JSON, the result is directly the returned JSON object.
*/
// Call the method without Parameters
$ ("# Btnhelloworld"). Click (function (){
$. Ajax ({
Type: "Post ",
Contenttype: "application/JSON ",
URL: "webservice1.asmx/helloworld ",
Data :"{}",
Datatype: 'json ',
Success: function (result ){
Alert (result. D );
}
});
});
// Input 1 parameter
$ ("# Btnhello"). Click (function (){
$. Ajax ({
Type: "Post ",
Contenttype: "application/JSON ",
URL: "webservice1.asmx/Hello ",
Data: "{Name: 'kimogigii '}",
Datatype: 'json ',
Success: function (result ){
Alert (result. D );
}
});
});
// Returns the generic list
$ ("# 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 ("| "));
}
});
});
// Return complex types
$ ("# 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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.