javascript jsonp 请求中的“未捕获的类型错误:无法读取未定义的属性‘toLowerCase’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24044813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:03:40  来源:igfitidea点击:

"Uncaught TypeError: Cannot read property 'toLowerCase' of undefined" on jsonp request

javascriptjquerynode.jsjsonp

提问by Julio Marins

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:

我有一个节点服务器和一个来自另一台服务器的应用程序,它通过 jquery 执行 AJAX jsonp 请求以获取参数的 json 数组,但是当我请求数据时,jquery 抛出此错误:

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

请求已发送,我得到响应,但我无法在 javascript 客户端获取数据

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:

客户端的Javascript:

$(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

我使用的是 jquery 2.1.0,但我尝试过其他版本并且错误仍然存​​在

My question is how i can get the data in the success clause

我的问题是如何获取成功子句中的数据

采纳答案by Oleg

JSONP response differs from JSON response. With JSONP you actually respond with a JavaScript code.

JSONP 响应不同于 JSON 响应。使用 JSONP,您实际上使用 JavaScript 代码进行响应。

So you have to modify your code like this:

所以你必须像这样修改你的代码:

First, require querystringmodule in the top of your script:

首先,需要querystring脚本顶部的模块:

var qs = require('querystring');

And then you have to rewrite your HTTP request handler:

然后你必须重写你的 HTTP 请求处理程序:

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

您可以在此处了解有关 JSONP 的更多信息

回答by jylauril

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.

您不应该在没有任何参数的情况下调用 getResponseHeader。从技术上讲,错误在于 jQuery 在调用 toLowerCase() 之前不会检查键是否作为参数给出,但是,如果没有给出参数,实际的 getResponseHeader 实现也会抛出错误。

If you meant to just return ALL the response headers, you should use:

如果您只想返回所有响应标头,则应使用:

console.log(data.getAllResponseHeaders())