Ajax calls using jquery are always error-tested solutions _ajax related

Source: Internet
Author: User
Using jquery's AJAX functionality to invoke a page, you find that there is always an error.
JS code is as follows
Copy Code code as follows:

$.ajax ({
Type: ' Post ',
URL: ' testdata.aspx ',
Data: {
Language_type: ' CN '},
DataType: ' JSON ',
Success:function (data) {
Alert (' OK ');
},
Error:function (data) {
Alert (' Err ');
});
}

The testdata.aspx code is as follows, omitting some of the code behind
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "TestData.aspx.cs" inherits= "Demand_testdata"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The TestData.aspx.cs code is as follows
Copy Code code as follows:

public partial class Demand_TestData:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
List<testc> t=new list<testc> ();
for (int i = 0; i < i++)
{
TESTC C = new TESTC ();
c.rd_id = i;
T.add (c);
}
String test = Jsonconvert.serializeobject (t);
Response.ContentType = "Application/json";
Response.Write (test);
Response.Clear ();
}
}
public class TESTC
{
public int rd_id
}

But just a few lines of code, jquery always complains, if the DataType: ' JSON ', modified to other types will not be wrong, very strange, but my system, what is needed is ' JSON object
Finally, after repeated tests to find that the code to modify the following places is finally normal
1 Remove the Response.Clear () from the TestData.aspx.cs page; what's not clear?
2 Modifying the class TESTC to the following form, which is to add the Read settings property to the field
Copy Code code as follows:

public class TESTC
{
private int rd_id;
public int rd_id
{
get {return rd_id;}
set {rd_id = value;}
}
}

3 Delete all the code in the Testdata.aspx page, leaving only the first line and the modified code as follows
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "TestData.aspx.cs" inherits= "Demand_testdata"%>

After so many tests, it's finally normal.
I've found that using jquery's Ajax often skips back into the wrong process, especially dataType: ' json ', it seems that jquery has a very strict validation mechanism, it looks like this must be noted, otherwise it will be wrong
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.