Jquery + Asp. Net ajax small test

Source: Internet
Author: User

I tried to use jquery's ajax, so I did a test. However, I started to think that the error message "parsererror" is returned as a json format problem, and finally found that it was a web. I lost two lines in config and got depressed for a long time. I read a greki article from a netizen and expressed my thanks here. But I did not do it. I asked my friend for a test, finally, we can!

<HttpHandlers>
<Remove verb = "*" path = "*. asmx"/>
<Add verb = "*" path = "*. asmx "validate =" false "type =" System. web. script. services. scriptHandlerFactory, System. web. extensions, Version = 1.0.61025.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35 "/>
</HttpHandlers>

Below are some examples of the test:

The test (vs05) uses a static file, a jquery1.3.2.js file, and a WebServices file.

A static file test

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "test. aspx. cs" Inherits = "test" %>
<! 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 id = "Head1" runat = "server">
<Title> No title page </title>
<Script language = "javascrpt" type = "text/javascript" src = "/js/jquery1.3.2.js"> </script>
<Script language = "javascript" type = "text/javascript">
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Input id = "Button1" type = "button" value = "button"/>
<Span id = "span1"> </span>

</Form>
</Body>
</Html>

Js is used in static files (in four cases ):

1. Without Parameters
$ (Document). ready (function (){
$ ('# Button1'). click (function (){
$. Ajax ({
Type: "POST", // use the get method to access the background
DataType: "json", // return data in json format
ContentType: "application/json ",
Url: "WebService. asmx/HelloWorld", // the backend address to be accessed
Data :"{}",
Success: function (result ){
Alert (result );
},
Error: function (xhr, msg, e) {alert (msg );}
});
});
});

2. With Parameters
$ (Document). ready (function (){
$ ('# Button1'). click (function (){
$. Ajax ({
Type: "POST", // use the get method to access the background
DataType: "json", // return data in json format
ContentType: "application/json ",
Url: "WebService. asmx/HelloName", // the backend address to be accessed
Data: "{name: 'lqingta '}",
Success: function (result ){
Alert (result );
},
Error: function (xhr, msg, e) {alert (msg );}
});
});
});

3. Return to the generic list
$ (Document). ready (function (){
$ ('# Button1'). click (function (){
$. Ajax ({
Type: "POST", // use the get method to access the background
DataType: "json", // return data in json format
ContentType: "application/json ",
Url: "WebService. asmx/GetArray", // the backend address to be accessed
Data: "{I: 5 }",
Success: function (result ){
Alert (result. join ("-"));
},
Error: function (xhr, msg, e) {alert (msg );}
});
});
});

4. Return complex types
$ (Document). ready (function (){
$ ('# Button1'). click (function (){
$. Ajax ({
Type: "POST", // use the get method to access the background
DataType: "json", // return data in json format
ContentType: "application/json ",
Url: "WebService. asmx/GetPerson", // The background address to be accessed
Data: "{name: 'lqt ', score: 80 }",
Success: function (result ){
Var person = result;
Var showText = [];
For (var p in person)
{
ShowText. push (p + ":" + person [p]);
}
Alert (showText. join ("\ r \ n "));
},
Error: function (xhr, msg, e) {alert (msg );}
});
});
});

Ii. webservice. cs File
Using System;
Using System. Web;
Using System. Collections;
Using System. Collections. Generic;
Using System. Web. Services;
Using System. Web. Services. Protocols;
Using System. Web. Script. Services;

/// <Summary>
/// Summary of WebService
/// </Summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[ScriptService]
Public class WebService: System. Web. Services. WebService {

Public WebService (){

// If you use the designed component, uncomment the following line
// InitializeComponent ();
}

[WebMethod]
Public string HelloWorld (){
Return "Hello World ";
}

[WebMethod]
Public string HelloName (string name)
{
Return string. Format ("Hello, {0}", name );
}

[WebMethod]
Public List <int> GetArray (int I)
{
List <int> list = new List <int> ();
While (I> = 0)
{
List. Add (I --);
}
Return list;
}

[WebMethod]
Public Person GetPerson (string name, int score)
{
Person person = new Person ();
Person. Name = name;
Person. Score = score;
Return person;
}

Public class Person
{
Private string _ Name;
Private int _ Score;
Public string Name
{
Get {return _ Name ;}
Set {_ Name = value ;}
}
Public int Score
{
Get {return _ Score ;}
Set {_ Score = value ;}
}
}


}

 

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.