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.