开发者

How to POST to a RESTful web service with jQuery AJAX?

been banging my head against the wall with this one.

Need to POST to a RESTful web service. Username goes in the request url. Password goes in the request body. Content-type must be application/x-www-form-urlencoded.

Using the Chrome "Simple REST Client" extension, everything is working fine.

Using jQuery.AJAX(), I continually get a 405 (Method Not Allowed) error.

Okay, here's the code:


    $.ajax({
       type: "POST",
       contentType: "application/x-www-form-urlencoded",
       url: baseURL + "api/users/" + username + "/login",     
       data: { passwo开发者_运维问答rd: password },
       success: function(data) {
            console.log("success ", data.response);
        },
        error: function(data) {
            console.log("error ", data.error);
        },
        dataType: "jsonp"               
    });

Does anyone see anything that is wrong with the code?

Thanks, Jacob


Jsonp isn't designed to be used with POST requests (see the answer to this question), so I suspect that the dataType: "jsonp" is causing the request to be sent as GET rather than POST. You can confirm this behaviour using the "net" panel in Firebug or the Chrome developer console.

What do you expect the server to return? You might be able to fix it by dropping the dataType, or setting it to some other value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜