jsonp中的'Uncaught SyntaxError:Unexpected token:' [英] The 'Uncaught SyntaxError: Unexpected token : ' in jsonp

查看:151
本文介绍了jsonp中的'Uncaught SyntaxError:Unexpected token:'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从服务器到浏览器获取了响应json数据,但是令人困惑的是,该数据无法在浏览器中显示,并且我在控制台中发现了一个错误,告诉我"Uncaught SyntaxError:Unexpected token:".这是我在节点js中的代码.

I have already get the response json data from server to browser, but it is confused that the data can not be displayed in the browser and I found the error in the console that told me 'Uncaught SyntaxError: Unexpected token :'. Here is my code in node js.

function callFacebook(){
$.ajax({
    url: "http://192.168.37.179:8888/facebook/connect?callback_=[TIMESTAMP]",
        type: "GET",
        dataType: "jsonp",
        jsonp:"jsonp",
    cache: true,
    timeout: 5000,
    success: function(data) {
        execute(data);
    },
    error:function() { console.log('Uh Oh!'); }
});

} 这是响应json数据: res.header('Content-Type','application/json'); res.header('Charset','utf-8'); res.send({"something": "father"});

} and here is the response json data: res.header('Content-Type','application/json'); res.header('Charset','utf-8'); res.send({"something": "father"});

推荐答案

从服务器上,您只发送普通的JSON数据,但是在客户端上,您期望使用JSONP. 来自服务器的响应不是JSONP,并且浏览器的确会因语法错误而引发异常.

From the server you are sending just normal JSON data, but on client you are expecting JSONP. The response from server is not JSONP and browser does throw exception as syntax is wrong.

如果您需要从服务器发送JSONP,则如果使用Express,请添加额外的use:

If you need to send JSONP from server, then if you are using express add extra use:

app.configure(function() {
  app.set('jsonp callback', true);
});

然后发送JSONP只需稍微更改res.您不再需要设置任何标头,因为它们会被自动检测到:

Then to send JSONP just slightly change res. You don't need to set any header anymore, as they will be automatically detected:

res.jsonp({ hello: 'world' });

在客户端,jQuery会自行添加回调,因此请使用以下简化方式:

On client side, jQuery will add callback it self, so use just this simplified way:

$.ajax({
  url: "http://192.168.37.179:8888/facebook/connect",
  type: 'GET',
  dataType: 'jsonp',
  cache: true,
  timeout: 5000,
  success: function(data) {
    console.log(data)
  },
  error: function(xhr, status, error) {
    console.log('error[' + status + '] jsonp');
  }
});

同样,如果您的节点通过nginx或任何其他Web平台代理,请不要忘记在此处启用utf8编码.

As well if your node is proxied through nginx or any other web platform, don't forget to enable utf8 encoding there.

这篇关于jsonp中的'Uncaught SyntaxError:Unexpected token:'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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