简体   繁体   English

如何在Express中读取jsonp数据

[英]How to read jsonp data in express

I'm trying to send data from phonegap to an express app. 我正在尝试将数据从Phonegap发送到Express应用程序。 Here is my code: 这是我的代码:

Phonegap: 电话空档:

    $.ajax({
        type: 'POST',
        url:"http://127.0.0.1:3000/test",
        data: {"test":"this works!"},
        dataType: 'jsonp',
        crossDomain: true,
        contentType: 'application/json',
        success: function(data){
            console.log('data successfully sent');
        },
        error: function(){
            console.log('there was an error');
        }
    });

Express: 表达:

    app.post('/ajax' , function (req , res){
    console.log(req.body)
    res.redirect('/test');
    });

Right now req.body is an empty object. 现在req.body是一个空对象。 I've included express.bodyParser() at the top of my stack, and have also tried JSON.parse(req.body) and JSON.stringify(req.body) , but neither have worked. 我已经在堆栈顶部添加了express.bodyParser() ,并且还尝试了JSON.parse(req.body)JSON.stringify(req.body) ,但是都没有用。

Can anybody think of something else to try? 有人可以考虑尝试其他方法吗? Do I need to do something different because it's jsonp? 我需要做一些不同的事情,因为它是jsonp吗?

Please let me know. 请告诉我。 Thanks! 谢谢!

The jquery: jQuery的:

$.getJSON(
    "http://127.0.0.1:3000/test?callback=?", 
    {"test":"this works!"},
    function(data){
        // data should be {foo: "bar"}
        // ... DO CODE
    }
);

The node: 节点:

app.get('/test' , function (req , res){
    // req.query should be {test: "this works!"}
    // ... DO CODE
    res.jsonp({foo: "bar"})
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM