简体   繁体   English

jsonp请求上的“ Uncaught TypeError:无法读取未定义的属性'toLowerCase'”

[英]“Uncaught TypeError: Cannot read property 'toLowerCase' of undefined” on jsonp request

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:password@example.com/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 我的问题是我如何获取成功子句中的数据

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. 您不应该在没有任何参数的情况下调用getResponseHeader。 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. 从技术上讲,错误是jQuery在调用toLowerCase()之前不检查键是否作为参数提供,但是,如果没有给出参数,则实际的getResponseHeader实现也会引发错误。

If you meant to just return ALL the response headers, you should use: 如果您只想返回所有响应头,则应使用:

console.log(data.getAllResponseHeaders())

JSONP response differs from JSON response. JSONP响应不同于JSON响应。 With JSONP you actually respond with a JavaScript code. 使用JSONP,您实际上会以JavaScript代码进行响应。

So you have to modify your code like this: 因此,您必须像这样修改代码:

First, require querystring module 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的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取属性toLowerCase的未定义 - Uncaught TypeError: Cannot read property toLowerCase of undefined 未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError无法读取未定义的属性“ toLowerCase” - Uncaught TypeError Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法读取未定义的属性'toLowerCase' - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 我有错误未捕获的TypeError:无法读取未定义的属性'toLowerCase' - I have the error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined 未捕获的TypeError:无法在文本字段上读取未定义错误的属性“ toLowerCase” - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined Error on text field 未捕获的类型错误:无法读取未定义的属性“toLowerCase”(待办事项列表应用程序) - Uncaught TypeError: Cannot read property 'toLowerCase' of undefined (Todo list application ) Elastic_Grid | “未捕获的TypeError:无法读取未定义的属性'toLowerCase'” - Elastic_Grid | “Uncaught TypeError: Cannot read property 'toLowerCase' of undefined” 获取错误未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - Getting error Uncaught TypeError: Cannot read property 'toLowerCase' of undefined TokenInput +未捕获的TypeError:无法读取未定义的属性“ toLowerCase” - TokenInput + Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM