admin管理员组文章数量:1326123
I have a node server and an application from another server doing a AJAX jsonp request via jquery to get a json array of parameters, but when i request the data the jquery throws this error:
Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
The request is sent and i get the response, but i can't get the data in the javascript client-side
Node server:
var my_http = require('http');
var databaseUrl = "leitordecarga"; // "username:[email protected]/mydb"
var collections = ["tables"]
var db = require("mongojs").connect(databaseUrl, collections);
var callback = [];
db.tables.find(function(err,values){
if(err || !values){
console.log('error');
}else{
values.forEach(function(value){
callback.push(value);
});
}
});
my_http = require("http");
my_http.createServer(function(request,response){
response.writeHeader(200, {"Content-Type": "application/json"});
response.write(JSON.stringify(callback));
response.end();
}).listen(8080);
Javascript in client-side:
$(document).ready(function(){
$.ajax({
url:'http://localhost:8080/mongo.js',
dataType:'jsonp',
type: "GET",
jsonp : "callback",
contentType: "application/jsonp",
success: function(data){
console.log(data);
},
error: function(data){
console.log(data.getResponseHeader());
}
}).done(function( data ) {
console.log(data);
});
});
im using jquery 2.1.0 but i've tried other versions and the error persists
My question is how i can get the data in the success clause
I have a node server and an application from another server doing a AJAX jsonp request via jquery to get a json array of parameters, but when i request the data the jquery throws this error:
Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
The request is sent and i get the response, but i can't get the data in the javascript client-side
Node server:
var my_http = require('http');
var databaseUrl = "leitordecarga"; // "username:[email protected]/mydb"
var collections = ["tables"]
var db = require("mongojs").connect(databaseUrl, collections);
var callback = [];
db.tables.find(function(err,values){
if(err || !values){
console.log('error');
}else{
values.forEach(function(value){
callback.push(value);
});
}
});
my_http = require("http");
my_http.createServer(function(request,response){
response.writeHeader(200, {"Content-Type": "application/json"});
response.write(JSON.stringify(callback));
response.end();
}).listen(8080);
Javascript in client-side:
$(document).ready(function(){
$.ajax({
url:'http://localhost:8080/mongo.js',
dataType:'jsonp',
type: "GET",
jsonp : "callback",
contentType: "application/jsonp",
success: function(data){
console.log(data);
},
error: function(data){
console.log(data.getResponseHeader());
}
}).done(function( data ) {
console.log(data);
});
});
im using jquery 2.1.0 but i've tried other versions and the error persists
My question is how i can get the data in the success clause
Share Improve this question edited Jun 4, 2014 at 18:26 Joe 15.6k8 gold badges50 silver badges57 bronze badges asked Jun 4, 2014 at 18:23 Julio MarinsJulio Marins 10.7k8 gold badges52 silver badges54 bronze badges 7-
4
Which line of code does the error point to?
toLowerCase
is not mentioned directly in any of your code in the question. – Rory McCrossan Commented Jun 4, 2014 at 18:26 - 1 Well that is not a JSONP response ing back, that would be your first problem. – epascarello Commented Jun 4, 2014 at 18:29
- the error is thrown from an error in the jquery lib – Julio Marins Commented Jun 4, 2014 at 18:29
- o tried response.writeHeader(200, {"Content-Type": "application/jsonp"}); in the server-side but i get this: Resource interpreted as Script but transferred with MIME type application/jsonp: "localhost:8080/…". – Julio Marins Commented Jun 4, 2014 at 18:30
- You are MAKING A JSONP request and you are returning JSON...that is your problem. The client is expecting a javascript file, not json. Do you know what JSONP is? – epascarello Commented Jun 4, 2014 at 18:34
2 Answers
Reset to default 4The problem in this case seems to be this line:
console.log(data.getResponseHeader());
You're not supposed to call the getResponseHeader without any parameters. The error is technically that jQuery doesn't check if the key was given as an argument before calling toLowerCase() to it, however, the actual getResponseHeader implementation also throws an error if no parameters were given.
If you meant to just return ALL the response headers, you should use:
console.log(data.getAllResponseHeaders())
JSONP response differs from JSON response. With JSONP you actually respond with a JavaScript code.
So you have to modify your code like this:
First, require querystring
module in the top of your script:
var qs = require('querystring');
And then you have to rewrite your HTTP request handler:
my_http.createServer(function(request,response){
var funcName = qs.parse(request.url).callback;
response.writeHeader(200, {"Content-Type": "text/javascript"});
response.write(funcName + '(' + JSON.stringify(callback) + ');');
response.end();
}).listen(8080);
You can learn more about JSONP here
本文标签:
版权声明:本文标题:javascript - "Uncaught TypeError: Cannot read property 'toLowerCase' of undefined" on jsonp re 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742192143a2430410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论