jQuery ajax:未捕获的TypeError:无法读取属性&to; toCowerCase'未定义的

时间:2015-08-03 12:28:50

标签: jquery ajax

我阅读了一些与此问题相关的帖子,但遗憾的是,我的案例找不到解决方案

我遇到错误 jquery.min.js:5未捕获的TypeError:无法读取属性' toLowerCase'当我发出以下请求时,未定义 jQuery ajax

ajax_test.htm



<script src="common_test.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
//String.prototype.trim = function(){return
//(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}
$(document).ready(function() {
	formDownloadFile();
});

function formDownloadFile() {
	// jQuery ajax:
	alert("sending ajax...");
	jQuery.ajax({
        type: 'POST',
        url: 'ajax_test2.htm',
		dataType: "text",
        data: {}
	})
    .done(function(data, textStatus, jqXHR) {	// Succes
		console.log('done => data: ', data, ', textStatus: ', textStatus, ", jqXHR: ", jqXHR);
    })
	.fail(function(jqXHR, textStatus, errorThrown) {	// fail
		console.error('fail => jqXHR: ', jqXHR, ', textStatus: ', textStatus, ", error: ", errorThrown);
	})
	.always(function() {	//
		
	});
	
	alert("ajax sended");
}
</script>
&#13;
&#13;
&#13;

common_test.js :这个库包含很多javascript函数,但我发现我的问题是由以下函数引起的

&#13;
&#13;
... my stuffs ...
String.prototype.trim = function(){return (this.replace(/^[\s\xA0]+/,
   "").replace(/[\s\xA0]+$/, ""))};
... my others stuffs ...
&#13;
&#13;
&#13;

ajax_test2.htm 什么都没有!

当我从 ajax_test.htm 中删除 common_test.js 时,我没有更多问题! 我不明白为什么?

非常感谢 最好的祝福 DSEA

2 个答案:

答案 0 :(得分:0)

基本上你在这里要做的是将common_test.js中的字符串替换为小写字母,因此产生错误。解决这个问题的一种方法是:

String.prototype.trim = function(){return (this.replace(/^[\s\xA0]+/,
   "").replace(/[\s\xA0]+$/, "").toLowerCase())};

您需要将字符串显式转换为小写,您可以通过明确定义toLowerCase();

来实现

&#13;
&#13;
<script src="common_test.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
String.prototype.trim = function(){return
(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "").toLowerCase())}
$(document).ready(function() {
	formDownloadFile();
});

function formDownloadFile() {
	// jQuery ajax:
	alert("sending ajax...");
	jQuery.ajax({
        type: 'POST',
        url: 'ajax_test2.htm',
		dataType: "text",
        data: {}
	})
    .done(function(data, textStatus, jqXHR) {	// Succes
		console.log('done => data: ', data, ', textStatus: ', textStatus, ", jqXHR: ", jqXHR);
    })
	.fail(function(jqXHR, textStatus, errorThrown) {	// fail
		console.error('fail => jqXHR: ', jqXHR, ', textStatus: ', textStatus, ", error: ", errorThrown);
	})
	.always(function() {	//
		
	});
	
	alert("ajax sended");
}
</script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

最后,它是因为我定义的函数“String.prototype.trim”在2行引起的:

String.prototype.trim = function(){return
(this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}

如果我换上一行:

    String.prototype.trim = function(){	return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, "")) }

或使用'good'以下语法:

String.prototype.trim = function(){
	return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
}

我没有更多问题 希望它对某人有帮助:))

最好的问候

相关问题