【问题标题】:Ajax JSONP Express parseError from Safari Extension来自 Safari 扩展的 Ajax JSONP Express parseError
【发布时间】:2013-03-17 18:20:23
【问题描述】:

首先,我在这里阅读了很多帖子,但在我自己的代码中没有发现问题,包括这个$.ajax and JSONP. ParseError and Uncaught SyntaxError: Unexpected token :

我正在构建一个 Safari 扩展,需要发布/获取到我的服务器并处理响应。 Safari 抛出此错误:

SyntaxError: Unexpected token ':'

还有这条消息

"handle was not called"

其中“handle”是此扩展代码中的回调:

var server="http://localhost:3001/api/login";
$.ajax({
       type : "GET",
       url :  server,
       data: {"something" : "else"}
       dataType: 'jsonp',
       jsonp:false,
       jsonpCallback: 'handle',
       success: function(data, text){
        var json = $.parseJSON(data);
        console.log(json)
       },
       error: function (request, status, error) {
        console.log(error );
       }  
});

Express.js (2.5.5) 代码是:

//in the config
app.set( "jsonp callback", true )

app.all('/', function(req, res, next){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  next();
});

app.get('/api/login', function(req, res){
res.json(
  {
    "success": {"that":"this"}
  }
);
});

注意:我尝试过 res.jsonp、设置内容类型等,但响应相同。在这个过程中,我学到了很多关于 CORS 和 Ajax 的知识,但我的眼睛显然是模糊的。点击我的高跟鞋三下也没有帮助。

线索?谢谢!

【问题讨论】:

    标签: ajax safari express jsonp


    【解决方案1】:

    通过设置dataType: 'jsonp',它已经为你解析了JSON。 jsonp: true 不正确。这个组合应该可以工作:

    JSONP

    $.ajax({
       url : "http://localhost:3001/api/login",
       data: {"something" : "else"},
       dataType: 'jsonp',
       success: function(data){
         // It is already an object, don't parse it again.
         console.log(data)
       },
       error: function (request, status, error) {
         console.log(error );
       }  
    });
    

    app.get('/api/login', function(req, res){
      res.jsonp({
        "success": {"that":"this"}
      });
    });
    
    // Remove this:
    app.set( "jsonp callback", true )
    

    CORS 浏览器和 JSON:

    $.ajax({
       url : "http://localhost:3001/api/login",
       data: {"something" : "else"},
       dataType: 'json',
       success: function(data){
         // It is already an object, don't parse it again.
         console.log(data)
       },
       error: function (request, status, error) {
         console.log(error );
       }  
    });
    

    // This is 'app.use', not 'app.all'.
    app.use('/', function(req, res, next){
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "X-Requested-With");
      next();
    });
    
    app.get('/api/login', function(req, res){
      res.json({
        "success": {"that":"this"}
      });
    });
    

    【讨论】:

    • 谢谢--测试了你的第一个例子:这又回到了你评论的我的另一个 Q,再次得到了双重功能:jQuery19108954158783890307_1364399791627 && jQuery19108954158783890307_1364399791627({ "success": { "that" } });测试其他人......(另外,答案中缺少逗号,将编辑
    • 第二个例子给出了这个错误(这是一个扩展):XMLHttpRequest cannot load localhost:3001/api/login?something=else。 Access-Control-Allow-Origin 不允许来源example.com。我已将扩展权限设置为允许所有网站... PermissionsWebsite AccessInclude Secure Pages级别全部
    • @bear 不幸的是,我对 Safari 扩展了解不多。 example.com 来自哪里?
    • example.com 只是我的本地主机。该扩展程序从您当前所在的任何站点获取数据并将其发布回该端点。
    • @bear 你真的在你的代码中使用'example.com'吗?该主机名已保留。
    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 2019-08-11
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多