[英]jQuery.post() send email but doesn't gets to .done()/.fail()/.always() without server refresh
[英]jQuery & CORS always fail on an open remote server
我試圖在應該啟用CORS的遠程服務器上使用jQuery $.ajax()
方法模擬POST。
想法是提交帶有用戶名/密碼的表單,並從成功的身份驗證中獲取服務器提供的cookie。
這是jQuery:
$.ajax({
type: 'POST',
crossDomain: true,
async: false,
url: this.url,
data: {
username: this.username,
password: this.password
},
xhrFields: {
withCreditentials: true
},
success: function(responseData, textStatus, jqXHR) {
console.info(jqXHR.getAllResponseHeaders());
},
error: function(responseData, textStatus, errorThrown) {
console.warn(errorThrown);
}
});
這是響應頭:
HTTP/1.1 200 OK
Connection: close
Content-Type: text/html
Pragma: no-cache
Cache-control: no-cache,max-age=0,must-revalidate
Content-Encoding: gzip
Vary: Accept-Encoding
Transfer-Encoding: chunked
和請求標頭:
Accept: * / *
Accept-Encoding: gzip, deflate, sdch
Accept-Language: fr,en-US;q=0.8,en;q=0.6
Cache-Control: max-age=0
Connection: keep-alive
Host: my remote server (URL is hidden)
問題是我得到一個console.warn告訴我:
Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'URL'
我還嘗試了使用可模擬POST的Chrome擴展程序:我將與jQuery中相同的數據放入jQuery中,並且成功完成了所有工作,從調用到提交表單后發送的cookie。
我不明白我的jQuery如何無法加載URL,但無論如何都會給我200 OK ! 而且我不知道該怎么做!
我在哪里做錯了?
看來您需要在api響應標頭中添加Access-Control-Allow-Origin: *
標頭。 您的REST客戶端(鑲邊擴展程序)不遵守瀏覽器之類的CORS規則,這就是它起作用的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.