【问题标题】:jquery to node : POST seen as GETjquery 到节点:POST 被视为 GET
【发布时间】:2013-12-26 15:30:10
【问题描述】:

我正在使用 Phonegap 创建一个 android 移动应用程序,所以我决定使用 jQuery 移动应用程序。服务器端将由 node.js 应用程序处理。

在客户端,我让 javascript 发送一个 POST 请求:

function validate() {
            //alert($('#username').val());
            var uname = $('#username').val() ;
            var pword = $('#password').val() ;
            $.ajax({
                type : "POST",
                dataType: "jsonp",
                jsonpCallback: "responding",
                url : "http://localhost:8888/authenticate",
                data :  { username: uname , password : pword },
                success : function(data) {
                    alert(data);
                },
                error : function(jqXHR, textStatus, errorThrown) {
                    alert("Error, status = " + textStatus + ", " + "error thrown: " + errorThrown);
                }
            });
        }

在服务器端,节点使用

function authenticate(request, response) {
console.log("Request handler 'upload' was called.");
console.log(request);
response.writeHead(200, {
    "Content-Type" : "text/plain"
});
response.write('responding(\'{"message": "Dummy Reply!"}\')');
response.end();

}

当我绑定 console.log(request) 如你所见时,在控制台中,连同很多东西,我得到了这个..

url: '/authenticate?callback=responding&username=foo&password=bar&_=1388071403212', 方法:'GET',

POST 方法在服务器端是如何变成 GET 方法的??

您还可以告诉我如何在节点端的 POST 请求中获取参数。

【问题讨论】:

    标签: javascript android node.js jquery jquery-mobile


    【解决方案1】:

    你不能 POST 一个 JSONP 请求。

    type : "POST",
    dataType: "jsonp",
    

    JSONP 请求几乎总是GET(jQuery 只是将<script>)标签附加到DOM)(同域请求可能有一些例外)

    【讨论】:

    • 但是dataType curresponds to response不是吗?
    • @JamsheedKamarudeen 我建议在api.jquery.com/jQuery.ajax 阅读它的确切功能。另外,请阅读 jQuery 源代码以查看更多详细信息。
    • 既然不是同源,我没有别的选择,只能使用jsonp不是吗?
    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 2013-10-03
    • 2016-12-19
    • 2017-03-16
    • 2012-02-23
    • 2020-11-19
    • 2018-12-17
    • 2016-03-02
    相关资源
    最近更新 更多