簡體   English   中英

“ Ajax調用中出現” NetworkError:405 Method Not Allowed錯誤“

[英]"NetworkError: 405 Method Not Allowed error in Ajax Call

我創建了REST服務,並在Chrome REST Console上對其進行了測試。 那里工作正常,我得到響應,但是在通過jQuery Ajax消耗響應的同時,我得到"NetworkError: 405 Method Not Allowed error in Ajax CallCross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:64132/Auth. (Reason: CORS request failed). "NetworkError: 405 Method Not Allowed error in Ajax Call Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:64132/Auth. (Reason: CORS request failed).錯誤Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:64132/Auth. (Reason: CORS request failed).這是我的Ajax代碼。

var UserDetails = { "UserName": "Bond", "Password": "password" };

$.ajax({
    type: 'POST',
    url: 'http://localhost:64132/Auth',
    crossDomain: true,
    data: UserDetails,
    contentType: "application/json; charset=utf-8",
    success: function (data) {

        alert(data.UserName);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("some error");
    }
});

請幫助我解決此問題,並成功致電服務中心。謝謝。

更新

當我嘗試從Chrome調用服務時,控制台出現以下錯誤。

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
Test.html:1 XMLHttpRequest cannot load http://localhost:64132/Auth. No  'Access-Control-Allow-Origin' header is present on the requested resource.  Origin 'null' is therefore not allowed access. The response had HTTP status code 405.

現代瀏覽器對於從不同“域”檢索內容具有非常嚴格的規則。 從本地文件系統(您的HTML /腳本)中檢索內容與向localhost發出網絡請求(您的Ajax調用)不同,因此Chrome將其視為跨域請求。

為了使跨域請求成功,您或者需要同時從http://localhost:64132/ URL提供HTML內容/腳本(因此它們在同一個域中),或者您需要設置設置REST服務以實施CORS舞蹈以允許您的file: URL對其進行訪問。

如果不了解有關如何實現REST服務的詳細信息,就不可能針對這兩種選擇提供具體說明。 您選擇哪種方式將取決於您打算在現實生活中(無論是否具有相同域)部署頁面和服務的方式以及實現的難易程度。 如果您的REST服務部署在某種容器中,則它可能會提供用於實現CORS和/或為初始頁面提供服務的選項。

相同問題的隨機示例(盡管此處的確切解決方案不太適合您的特定情況): 嘗試將數據發布到LOCAL應用程序時,Origin為“ null”時的Access-Control-Allow-Origin標頭

暫無
暫無

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

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