Using jquery's ajax call is always an incorrect solution.

Source: Internet
Author: User

When jquery ajax is used to call a page, errors always occur.
The js Code is as follows:
Copy codeThe Code is 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, and the following code is omitted.
Copy codeThe Code is 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 codeThe Code is 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 <10; 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 in just a few lines of code, jquery always reports an error. If you change ype: 'json' to another type, it will not be wrong. It's strange, but in my system, the 'json object is required.
Finally, after repeated tests, we found that the Code in the following areas was changed and finally normal.
1. Remove Response. Clear () from the TestData. aspx. cs page. The specific reason is unclear.
2. Modify the TestC class in the following format, that is, add the read setting attribute to the field.
Copy codeThe Code is 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 on the TestData. aspx page. Only the first line is retained. The modified code is as follows:
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "TestData. aspx. cs" Inherits = "Demand_TestData" %>

After so many tests, it is finally normal.
I found that ajax using jquery often redirects back to the error processing process, especially dataType: 'json'. It seems that jquery has a strict verification mechanism. It seems that this must be noted, otherwise, an error occurs.

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.