ajax error的原因:1、dataType类型错误;2、json格式错误;3、async请求同步异步设置有问题;4、data参数值没有设置;5、传递参数的编码格式有问题;6、URL路径中有中文等。
本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。
一个标准的jquery的ajax代码:
$.ajax({ type: 'POST', url: 'getSecondClassification', data: {"sort2": sort2,"sort3":sort3)}, dataType: 'json', //contentType:"utf-8",发送请求的编码方式 //contentType都是默认的值:application/x-www-form-urlencoded contentType: 'application/json', data: JSON.stringify({a: [{b:1, a:1}]}) success: function (sort) { $("#noneSelect").remove(); var optionstring = ""; for (var i in sort) { optionstring += "<option value=\"" + sort[i].classId + "\" >" + sort[i].className + "</option>"; } $("#secondtype").html(optionstring); $("#build").removeAttr("disabled"); }, error: function (XMLHttpRequest, textStatus, errorThrown) { // $("#p_test").innerHTML = "there is something wrong!"; // alert(XMLHttpRequest.status); // alert(XMLHttpRequest.readyState); // alert(textStatus); } });
出现任何错误都会跳到error函数里
举例说明一些错误原因:
1. dataType错误
类型错误:后台返回的dataType类型和前台写的不一致会跳入error
格式错误:jquery1.4之后对json的格式要求非常严格,json格式错误也会跳入error.{"test":1} 注意格式
有时,在不需要返回值的情况下,扔按模板格式,设置了dataType:"json",参数;这时候,ajax传值正确时,出现200返回成功状态下报错的特殊情况。
2. async请求同步异步问题
async默认是true(异步请求),如果想一个Ajax执行完后再执行另一个Ajax, 需要把async=false
例如,你用post请求传值到另一个页面后台,但是页面一加载你的ajax就已经执行过了,传值接收是在后台才完成的,这时候就请求不到数据,所以可以考虑把ajax请求改为同步试试。
3. data不能不写
data为空也一定要传"{}";不然返回的是xml格式的。并提示parsererror. data:"{}"
parsererror的异常和Header 类型也有关系。及编码header('Content-type: text/html; charset=utf8');
4. 传递的参数
必须是ajax支持的编码格式
5. URL路径问题
路径不能有中文
用error调试错误信息:
可以alert出来,查看具体是哪里出错了
具体参数:
XMLHttpRequest:XMLHttpRequest.readyState: 状态码的意思 0 - (未初始化)还没有调用send()方法 1 - (载入)已调用send()方法,正在发送请求 2 - (载入完成)send()方法执行完成,已经接收到全部响应内容 3 - (交互)正在解析响应内容 4 - (完成)响应内容解析完成,可以在客户端调用了
XMLHttpRequest:XMLHttpRequest.status:
textStatus:错误原因
errorThrown:(可选)捕获的错误对象
【相关教程推荐:AJAX视频教程】
以上是ajax error的原因有哪些的详细内容。更多信息请关注PHP中文网其他相关文章!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互动,可及可访问。1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒体综合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityAncccossibility.2)和TagsallowsemplowsemplowseamemelesseamlessallowsemlessemlessemelessmultimedimeDiaiiaemediaiaembedwitWithItWitTplulurugIns.3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrevulnerabilityLikeClickLickLickLickLickLickjAckJackJacking.2)

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

使用ID选择器在CSS中并非固有地不好,但应谨慎使用。1)ID选择器适用于唯一元素或JavaScript钩子。2)对于一般样式,应使用类选择器,因为它们更灵活和可维护。通过平衡ID和类的使用,可以实现更robust和efficient的CSS架构。

html5'sgoalsin2024focusonrefinement和optimization,notnewfeatures.1)增强performandemandeffifice throughOptimizedRendering.2)risteccessibilitywithrefinedibilitywithRefineDatientAttributesAndEllements.3)expliencernsandelements.3)explastsecurityConcerns,尤其是withercervion.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供应,2)语义结构,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,简化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载
最流行的的开源编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境