Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the solution to always testing errors in ajax calls using jquery?

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

本篇文章给大家分享的是有关使用jquery 的ajax调用总是错误亲测的解决方法是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

使用jquery 的ajax功能调用一个页面,却发现总是出现错误。

js代码如下

代码如下:

$.ajax({

type: 'post',

url: 'TestData.aspx',

data: {

language_type: 'cn'},

dataType: 'json',

success: function (data) {

alert('ok');

},

error: function (data) {

alert('err');

});

}

TestData.aspx代码如下,省略了后边一些代码

代码如下:

TestData.aspx.cs代码如下

代码如下:

public partial class Demand_TestData : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

List t=new List ();

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

}

可就是这么短短几行代码,jquery总是报错,如果把 dataType: 'json', 修改为其他类型就不会错了,很奇怪,可我的系统里,需要的是'json对象

最后经过反复测试才发现修改了如下地方的代码后终于正常了

1去掉TestData.aspx.cs页面中的Response.Clear();具体什么原因不清楚

2修改类TestC为如下形式,也就是给字段添加了读取设置属性

代码如下:

public class TestC

{

private int rD_ID;

public int RD_ID

{

get { return rD_ID; }

set { rD_ID = value; }

}

}

3删除TestData.aspx页面中的所有代码,只保留第一行,修改后的代码如下

代码如下:

经过这么多测试终于正常了

我发现使用jquery的ajax的经常回跳入错误的处理流程,尤其是 dataType: 'json',看来jquery有很严格的验证机制,看来这个一定要注意,否则就会错误

以上就是使用jquery 的ajax调用总是错误亲测的解决方法是怎样的,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report