【问题标题】:catch response when send jsonp request phonegap发送 jsonp 请求 phonegap 时捕获响应
【发布时间】:2013-02-06 23:38:26
【问题描述】:

我通过 phonegap 编写应用程序 我用nodejs写的服务器端

exports.login = function(request, response) {
    var keys = Object.keys(request.query);
    request.body= JSON.parse(keys[1]);
    Accounts.findOne({
        name : request.body.name
    }, function(err, docs) {
        if (!docs) {
            response.json('{"error": "user-not-found"}');
        } else {
            console.log("docs: ", docs);
            Accounts.validatePassword(request.body.password, docs['hashedPass'], docs['salt'], function(error, res) {
                if (error) {
                    response.json(error);
                }
                if (res) {
                    generateToken(request.body.name, request.body.device, function(res) {
                        if (res) {
                            response.json('{"token": "' + res + '"}');
                        } else {
                            response.json('{"error": "cant-create-token"}');
                        }
                    });
                } else {
                    response.json('{"error": "invalid-password"}');
                }
            });

        }
    })
}

Phonegap:我写函数来登录

function LoginUser(info)
{
    var varUrl=server+"/auth/login";
    $.ajax({
            url:varUrl,
            type:"GET",
            contentType:"application/json",
            headers: {
                            Accept:"application/json",
                            "Access-Control-Allow-Origin": "*"
                        },              
            data:info,              
            dataType:"jsonp", 
            success:function(data)
                {
                    console.log("HERE");
                    console.log(data);                      
                },
            error: function(err){
                console.log(err);
            }


    });
}

我要求它会像这样http://localhost:3000/auth/login?callback=jQuery161014894121675752103_1361459462241&{%22name%22:%22fdfdfd%22,%22password%22:%22fdfdfdfd%22}&_=1361459615743

当我在 chrome 开发者标签网络的响应中看到 "{\"error\": \"user-not-found\"}"

但我无法在上面的function LoginUser(info) 中捕捉到这个回复 如何获取它,因为在控制台中它会打印 error: function(err){ console.log(err); }

我不知道为什么。 你能帮帮我吗?

【问题讨论】:

  • 你为什么使用GET请求,你应该使用POST登录。
  • 我试过get和post并得到同样的问题
  • jsonp 始终是一个获取请求。

标签: javascript ajax node.js cordova jsonp


【解决方案1】:

在服务器端,您必须使用回调并在您的响应中 你需要做的jsonp文件:

jQuery161014894121675752103_1361459462241({"error": "user-not-found"})

函数名称来自 jsonp 请求中的回调变量。 导致 jsonp 请求只是在您的站点中“包含”一个脚本标签,它可以执行 js(基本上)。

【讨论】:

  • 我只是尝试得到同样的问题 Uncaught ReferenceError: jQuery161014894121675752103_1361459462241 is not defined 并且打印错误:function(err){ console.log(err); }
  • 你必须在服务器端使用(假设是php): print $_REQUEST['callback']."({'error':'not logged in'})";
  • 我在服务器端使用nodejs
  • 你必须使用的函数是:localhost:3000/auth/…{%22name%22:%22fdfdfd%22,%22password%22:%22fdfdfdfd%22}&_=1361459615743变量@的内容987654323@在服务器端。
  • request.body= JSON.parse(keys[1]);信息太多,我只是通过上面的操作获取名称和密码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 1970-01-01
  • 2017-06-20
  • 1970-01-01
  • 2012-12-09
  • 2014-09-13
  • 1970-01-01
相关资源
最近更新 更多