Using JQuery Ajax and general processing programs to request data without refreshing, and how to debug errors, jqueryajax
With ajax () and general processing programs, you can request database data to ensure that the interface is no longer refreshed.
Jquery ajax Request Parameters description http://www.w3school.com.cn/jquery/ajax_ajax.asp
Code:
-
1 <! DOCTYPE html> 2 3
1 using System. web; 2 3 namespace DemoAjxa. ashx 4 {5 /// <summary> 6 // Handler1 abstract description 7 /// </summary> 8 public class Handler1: IHttpHandler 9 {10 11 public void ProcessRequest (HttpContext context) 12 {13 context. response. contentType = "text/plain"; 14 15 context. response. write ("Hello World"); 16} 17 18 public bool IsReusable19 {20 get21 {22 return false; 23} 24} 25} 26}
Effect:
The above is a post request to obtain a plain text string, if it is to obtain json data
You need to set the ajax parameter ype to josn. Generally, the processing program context. Response. Write (); The returned data type must also be in json format.
Parse the obtained json data and bind it to the interface.
Column: dataType: 'json', // data type returned by the expected Server
Data {name: 'hangenai'} If data has parameters,
In this way, the general handler can obtain the passed parameters.
String name = context. Request ["name"]. ToString ();
The returned request is error.
1. Check whether the url request address is correct
Firefox browser, install the plug-in Firebug, F12 can see the error shows the request address NotFound
2. data {} should be written in this way without passing parameters in real time. Otherwise, an error will be reported.
3. Whether the returned data type is consistent with that of dataType
If they are inconsistent, an error is returned.
4. check whether a cross-origin request has occurred.
5. Whether the encoding format is correct
Of course, you can also write it in the code to view the error output.
1 <script type = "text/javascript"> 2 $ (document ). ready (function () {3 $ ('# btn '). click (function () {4 $. ajax ({5 url: 'ashx/handler1.ashx', // string type parameter, sending request address 6 type: 'post', // Request Method (POST or get) the default value is get 7 contenType: "application/json; charset-utf-8", // Content Encoding type 8 dataType: 'text' when sending messages to the server ', // expected data type returned by the server 9 data: {name: 'hangenai'}, // The request data sent 10 success: function (data) {11 alert (data ); 12}, error: function (XMLHttpRequest) {13 alert ("Ajax request failed, error status:" + XMLHttpRequest. status); 14} 15}); 16}); 17}) 18 </script>
Effect:
XMLHttpRequest object
Attribute
ReadyState: Request status. The initial request value is 0 until the request is completed. The value increases to 4 (ReadyState has 5 states, 0 is not initialized, 1 is initialized, 2 is sent, 3 is received, and 4 is received.)
ResponseText: The response body received so far, readyState <3 this attribute is a Null String, = 3 is the current response body, = 4 is the complete response body
ResponseXML: the server parses the response as xml and returns the response as a Document object.
Status: status Code returned by the server, = 200 success, = 404 indicates "Not Found"
StatusText: the status of the server returned by the name. If "OK" is 200, "Not Found" is 400.