JQuery在Ajax响应上无法识别表达式
我有这个JQuery Ajax表单。
$('#modal-body-sign-in').on('submit', '#sign-in', function(e) { e.preventDefault(); var data = $(this).serialize(); var url = $(this).attr('action'); $.ajax({ //this is the php file that processes the data and send mail url : url, type : "POST", data : data, dataType: "html", //Do not cache the page cache : false, //success success : function(data) { //console.log(data.content); console.log($(data)); //$('#modal-body-sign-in').html(data); } }); })
关于响应,当我在console.log(data); in the console it shows me the responded html
console.log(data); in the console it shows me the responded html
但是当我执行此console.log($(data));
它给了我这个错误
未捕获错误:语法错误,无法识别的表达式:
ul { margin: 0%; } ul li { display: inline; list-style-type: none; float: left; } Insert title here jquery-1.9.0.min.js:2 a.error jquery-1.9.0.min.js:2 f jquery-1.9.0.min.js:2 x jquery-1.9.0.min.js:2 a jquery-1.9.0.min.js:2 st.fn.extend.find jquery-1.9.0.min.js:2 st.fn.st.init jquery-1.9.0.min.js:1 st jquery-1.9.0.min.js:1 $.ajax.success localhost:59 f jquery-1.9.0.min.js:1 p.fireWith jquery-1.9.0.min.js:1 r jquery-1.9.0.min.js:3 r
我这样做了
$(data)
因为我想获得响应的HTML页面的片段
自从我升级到jQuery 1.9.x后,我遇到了完全相同的问题。 实际上简单的$(数据)会产生崩溃,没有别的。 我曾经有过几次这个问题(没有以前的版本比1.9),但我不记得它的问题……
无论如何,这是一个完全阻塞的麻烦…仍在寻找原因和修复。
编辑:
如果我这样做:
$.ajax('/',function(html){ html = $('').append(html); $(html); });
它工作正常。 如果我做 :
$.ajax('/',function(html){ $(html); });
它给了我一个(通过FF错误控制台):
Erreur : Error: Syntax error, unrecognized expression: (...) jquery.min.js - line : 4
编辑2:
Ok经过长时间的搜索和测试后找到了解决方案: http : //stage.jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring
所以我现在做的事情如下:
$.ajax('/',function(html){ $($.parseHTML(html)); });
无论如何,jQuery总是让我的第一个元素与一个元素混淆,但至少它是有效的
如果您使用的是jQuery1.9,问题可能在于加载的内容。 有一个新的更新要求响应中的第一个字符是< [AKA 小于符号 ]。 即使是空格也会导致这种情况中断并抛出可怕的“未捕获的错误:语法错误,无法识别的表达式:”错误。
我建议在使用上面建议的解决方法之前检查一下。 这不是一个安全措施的错误。
http://jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring
- 选择JSTree检查节点是叶子还是只选择叶子
- 使用$ .ajax url传递多个参数
- jquery ajax readystate 0 responsetext status 0 statustext error
- 通过$ .getScript完成所有脚本加载后调用处理程序
- 使用HTTP基本身份validation的JQuery Ajax调用
- jQuery.ajax() – IE9中返回的未定义数据
- 使用“application / json;将ajaxpost发送到mvc; charset = utf-8“从vs web开发人员服务器返回错误500
- 如何在同一模块中调用函数?
- jQuery click事件与Firefox中的live函数表现不同