簡體   English   中英

jQuery.AJAX使用GET而不是POST

[英]jQuery.AJAX uses GET instead of POST

我有一個運行jQuery的非常簡單的HTML頁面,它正在嘗試發布到REST API。 這是我的代碼。

<script type="text/javascript" language="javascript">
    var user = 'someuser';
    var pass = 'somepassword';
    var payload = {
        "value1": "first value",
        "value2": "second value"
    };
    var rootUrl = 'http://someinternalserver:8888/api/Method';
</script>
<script language="javascript" type="text/javascript" id="postWtnBlock">
    function postValue_Go() {
        $.ajax({
            url: rootUrl,
            // Removing the next line or changing the value to 'JSON' results in an OPTIONS request.
            dataType: 'JSONP',
            data: JSON.stringify(payload),
            method: 'POST',
            user: user,
            password: pass,
            beforeSend: function (req) {
                req.setRequestHeader('Authorization', 'BASIC ' + btoa(user + ':' + pass));
            },
            success: function (data) {
                alert(data);
            },
            error: function (xhr) {
                alert(xhr.status + ' ' + xhr.statusText);
            }
        });
    }
</script>

這里發生兩件事。

1)每個請求都作為GET請求而不是POST發送。 2)授權標頭從不進入請求; 它總是不見了。

我不知道為什么會這樣。

更新#1:新的postValue_Go()看起來像這樣...

    function postValue_Go() {
        $.ajax({
            url: rootUrl,
            data: JSON.stringify(payload),
            method: 'POST',
            username: user,
            password: pass,
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                alert(data);
            },
            error: function (xhr) {
                alert(xhr.status + ' ' + xhr.statusText);
            }
        });
    }

這是Fiddler中捕獲的原始請求:

POST http://someinternalserver:8888/api/Method/ HTTP/1.1
Host: someinternalserver:8888
Connection: keep-alive
Content-Length: 693
Accept: */*
Origin: http://devserver
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://devserver/samples/ExternalAPI/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: someCookieValue=Boo; someOtherCookieValue=Yum;

{"value1": "first value","value2": "second value"}

原始的反應,也被提琴手捕捉到了。

HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 19 Aug 2016 17:12:29 GMT
Content-Length: 61

{"Message":"Authorization has been denied for this request."}

它是作為GET請求發送的,因為JSONP將腳本添加到您的網頁,並且src屬性指向Web服務(執行GET請求),然后返回包含請求數據的腳本,這些腳本都捆綁在回調函數中。

顯然,如果您願意對iframe等進行認真的工作,則可以通過JSONP執行POST請求(請參見此處 ),但是出於大多數目的和目的,您將僅限於GET請求。

永遠不會設置授權標頭,因為當前無法修改發送給腳本的標頭以添加到頁面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM