2012-04-19 132 views
6

我已經搜索了與類似問題有關的不同現有SO問題的加載,但是我一直未能找到與我的問題相關的任何內容。「405方法不允許」 - 當使用ASP.NET jQuery ajax POST

我有以下的jQuery代碼:

$.ajax({ 
    url: "Index.aspx/DeclineRequest" 
    , type: 'POST' 
    , contentType: 'application/json; charset=utf-8' 
    , dataType: 'json' 
    , data: JSON.stringify({ RequestId: requestId }) 
}); 

我使用了一個非常類似的技術張貼在另一個ASP.NET web應用程序的數據,這工作得很好。然而,這個應用程序返回以下錯誤:

"NetworkError: 405 Method Not Allowed - http://localhost:57255/ ....."

我無法找到任何有關這2所應用的不同,所以我很困惑,爲什麼這是行不通的。

這兩個應用程序我只能想到的唯一區別是,工作的是.NET 2.0,而不工作的是.NET 3.5。

我試着加入以下我web.config,但我仍然會得到相同的405錯誤:

<webServices> 
    <protocols> 
    <add name="HttpPost"/>  
    <add name="HttpPostLocalhost"/> 
    </protocols>  
</webServices> 

我怎樣才能解決這個問題?

UPDATE(16:40):我已將此應用程序移至IIS,並且不再返回405錯誤。我們的開發環境只是本地主機。爲什麼只在localhost上出現這個錯誤?

+0

你能告訴我們傻冒'DeclineRequest'方法? – 2012-04-19 12:01:11

+0

它是不是類似於http://stackoverflow.com/questions/7430492/twitter-search-api-networkerror-405-method-not-allowed ?? – 2012-04-19 12:01:37

+0

@ŁukaszW.pl'DeclineRequest'方法現在簡單地調用'return「Hello World」''。我不認爲這是問題,因爲輸入任何方法名稱都會返回相同的錯誤。我在一些測試中故意錯誤地檢查了這個方法。 – Curt 2012-04-19 12:58:59

回答

0

url:「Index.aspx/DeclineRequest」不應該是url:「Index.aspx?DeclineRequest」應該嗎?

對不起,有一個相當明顯的建議,我還沒有看到過這樣的網址。你正在改寫它?

+2

這是一個調用在Index.aspx代碼隱藏中定義的DeclineRequest方法的WebMethod調用格式。 – Dimitri 2012-04-19 12:12:17

+0

謝謝!我真的很想知道...... – 2012-04-19 12:13:41

1

405 errors often arise with the POST method. You may be trying to introduce some kind of input form on the Web site, but not all ISPs allow the POST method necessary to process the form.

All 405 errors can be traced to configuration of the Web server and security governing access to the content of the Web site, so should easily be explained by your ISP.

http://www.checkupdown.com/status/E405.html

我會變成IIS日誌記錄,以檢查請求。也讓小提琴手來看看是怎麼回事幕後

+0

我只是通過'localhost'來使用VS 2010,所以我沒有任何IIS日誌記錄。 – Curt 2012-04-19 13:01:24

+0

我看到了IIS和VS工作方式之間的很多不一致之處。我會嘗試設置IIS主機,並從那裏去。 – Dimitri 2012-04-19 13:06:14

+0

這對我的其他應用程序來說工作正常,儘管它運行在同一臺機器上,代碼幾乎相同。所以安裝IIS似乎不是解決方案。 – Curt 2012-04-19 13:08:10

0

覈查請求UR發送到服務器
的參數declineRequest是 的RequesId一個屬性或string(或其他數據類型)名稱的對象RequestId 的Cuz在後一種情況下美國可以只發送數據JSON或純字符串像RequestId=requestId
所以儘量用更合適

+0

即使沒有發送任何參數,我也會得到相同的問題 – Curt 2012-04-19 13:08:42

+0

究竟是什麼參數的類型呢?它是一些非空類型? – 2012-04-19 13:09:43

+0

它是一個整數' – Curt 2012-04-19 13:10:57

0

我的猜測是一些替代JSON.stringify({ RequestId: requestId }):在請求你說,返回的內容將是JSON,但你正在返回純字符串這是不正確的格式...嘗試返回正確的JSON或更改內容類型爲測試純文本...

-3

刪除此代碼,數據類型:從你的代碼「JSON」和運行

+0

這絕對不是正確的答案。數據類型與CORS錯誤無關。 – 2016-07-05 17:54:20