jQuery.ajax#get 之后出现意外的令牌冒号 JSON [英] Unexpected token colon JSON after jQuery.ajax#get

查看:28
本文介绍了jQuery.ajax#get 之后出现意外的令牌冒号 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 nodejs 上创建了一个极简的 API,它以 JSON 格式返回数据.

I have created a minimalist API on nodejs which return data in JSON format.

但是每次我尝试进行 ajax#get 调用并将我的 API 作为 URL 传递时,我都会收到一个错误,从 Chrome 判断,我收到一个 "Unexpected token :" 错误;

But every time I try to make a ajax#get call and pass my API as URL, I will get an error and judging from Chrome, I'm getting a "Unexpected token :" error;

这里是nodejs + express中的服务器代码:

here the server code in nodejs + express:

var
 http    = require( 'http' ),
 express = require( 'express' ),
 app      = express(),
 server  = http.createServer( app );

app.get( '/', function( req, res ) {
    console.log( 'req received' );
        res.setHeader('Content-Type', 'application/json');
    res.end( JSON.stringify({
    Name : "Tom",
    Description : "Hello it's me!"
    }) );

});

server.listen(3000, function() {
    console.log( 'Listening on 3000' );
});

"/" 返回的 JSON 是:{"Name":"Tom","Description":"Hello it's me!"}.

The JSON returned from "/" is: {"Name":"Tom","Description":"Hello it's me!"}.

这是我来自客户端 js 的调用:

Here is my call from the client js:

$.ajax({
    url: findUrl,
    type: 'get',
    dataType: 'jsonp',
    success: function ( data ) {
        self.name( data.Name );
        self.description( data.Description );
    },
    error: function( jqXHR, textStatus, errorThrown ) {
        alert(errorThrown);
    }
});

绘制错误时,我得到:"jQuery111108398571682628244_1403193212453 未被调用"

有人可以帮我吗?

我知道已经有人问过这个问题,但我还没有找到修复我的程序的解决方案.

I know this question has been asked already but I haven't manage to find a solution which fix my program.

推荐答案

支持JSONP 请求,服务器必须在响应中包含 PPadding".

To support JSONP requests, the server will have to include the P, or "Padding," in the response.

jQuery111108398571682628244_1403193212453({"Name":"Tom","Description":"Hello it's me!"})

语法错误,"Unexpected token :",是因为 JSONP 被解析为 JavaScript,其中 {...} 也代表 blocks.它只是利用 JSON 和 JavaScript 的类似语法来定义传递给全局函数调用的数据.

The syntax error, "Unexpected token :", is because JSONP is parsed as JavaScript, where {...} also represents blocks. It just takes advantage of JSON and JavaScript's similar syntax to define the data being passed to a global function call.

默认情况下,jQuery 将包含一个带有函数名称的 callback 查询字符串参数:

By default, jQuery will include a callback query-string parameter with the name of the function:

var callback = req.query.callback;
var data = JSON.stringify({
    Name : "Tom",
    Description : "Hello it's me!"
});

if (callback) {
    res.setHeader('Content-Type', 'text/javascript');
    res.end(callback + '(' + data + ')');
} else {
    res.setHeader('Content-Type', 'application/json');
    res.end(data);
}

ExpressJS 还包括 res.jsonp() 已经实现了这个条件:

ExpressJS also includes res.jsonp() that already implements this condition:

app.get( '/', function( req, res ) {
    console.log( 'req received' );

    res.jsonp({
        Name : "Tom",
        Description : "Hello it's me!"
    });
});

这篇关于jQuery.ajax#get 之后出现意外的令牌冒号 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆