1
votes

I read some posts related to this problem but unfortunately, no solution found for my case

i hit an error jquery.min.js:5 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined when i make a following request 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>

common_test.js: this library contains many javascript functions but i discovered that my problem is caused by the following function

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

ajax_test2.htm have nothing!

when i remove incluse common_test.js from ajax_test.htm, i have no more problem! i don't understard why?

thanks a lot best regards dsea

2
common_test.js has to have more content than that ?adeneo
But there's no toLowerCase in it, so it can't produce that erroradeneo
What is ajax_test2.htm?Barr J
had you tried it? it is an error from jquery.min.js:5 Uncaught TypeError: Cannot read property 'toLowerCase' of undefineddsea

2 Answers

0
votes

Basically What you are trying to do here is to replace the string in common_test.js to lower case letters and so it produce the errors.. One way to solve this is:

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

You need to explicitly convert your string to lower case, you do so by explicitly defining toLowerCase();

<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>
0
votes

Finally, it's caused because of my defined function "String.prototype.trim" is on 2 lines:

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

if I put on one line :

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

or with the 'good' following syntax:

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

i have no more problem hope it helps for someone :)

best regards