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

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

问题描述

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

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

但每次我尝试制作一个ajax#接到电话并将我的API作为网址传递,我会收到错误并从Chrome判断,我收到意外的令牌:错误;

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 中的服务器代码快递

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是: {姓名:汤姆,描述:你好,是我!}

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未被调用

When plotting the error I get: "jQuery111108398571682628244_1403193212453 was not called"

有人可以帮助我吗?

我知道这个问题已被提出但是我无法找到解决方案来修复我的程序。

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

推荐答案

支持 JSONP请求,服务器必须包含 P Padding ,在回复中。

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!"})

语法错误,意外的令牌:,是因为JSONP被解析为JavaScript,其中<​​code> {...} 也代表。它只是利用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 带有函数名称的query-string参数:

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天全站免登陆