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
 |  Show 2 more ments

2 Answers 2

Reset to default 4

The 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

本文标签: