2012-12-27 112 views
4

我是新來的Web服務隊,我得到當我試圖在Chrome控制檯上運行我的網頁(本地服務器以405狀態響應(不允許的方法)

錯誤以下錯誤

Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://localhost:12015/myWebService.asmx?op=GetCategories


下面是相關的代碼:

JQ uery

$.ajax({ 
    url: "http://localhost:12015/myWebService.asmx?op=GetCategories", 
    type: "POST", 
    ------------ 
    ------------ 
    success: function (data) {      
     var categories = data.d; 
     $.each(categories, function (index, category) { 
      category.CategoryId).text(category.CategoryName); 
     }); 
    }, 
    error: function (e) { //always executing 'error' only 
     alert("hello"); 
    } 

Web服務URL

http://localhost:12015/myWebService.asmx


[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public List<Categories> GetCategories() 
{ 
     //code 
} 

頁面URL

http://localhost:11761/Default.aspx

編輯:錯誤消失時,我只包括dataType: 'jsonp'但現在有另一個錯誤。

Uncaught SyntaxError: Unexpected token <
:12015/myWebService.asmx?op=GetCategories&callback=jQuery183010907560377381742_1356599550427&{}&_=1356599550438:3

當我點擊鏈接(錯誤中提到)時,它顯示頁面。那麼問題是什麼?我不知道錯誤的含義(以及顯示哪部分代碼)。請幫忙。

相關

Link1(解釋)
Link2(解決)

+0

http:// localhost:12015/myWebService.asmx < - 是否可以訪問,即時猜測這是一個web服務器問題,IIS配置可能 – mdcuesta

+0

@mdcuesta是的,對不起,我忘了提到一切都是本地的PC本身) –

+0

你的'dataType ='jsonp'? – Jai

回答

2

我加了jQuery AJAX調用

jQuery.support.cors = true; 

現在,它正在罰款我上面的朋友的建議簡單的一行: )

僅在IE BROWSER

我很樂意知道它是否可以用不同的方式解決,因爲它不是recommended

無論如何,我再次問這個問題,經過多次努力,我這是在我的情況下,它是失敗becasue還有在SERVET沒有POST方法解決了這個帖子here

2

試試這個

[WebMethod] 
[ScriptMethod(UseHttpPost = true)] 
public List<Categories> GetCategories() 
{ 
     //code 
} 

或編輯web.config

<system.web> 
    ... 
    <webServices> 
     <protocols> 
       <add name="HttpSoap"/> 
       <add name="HttpPost"/> --> 
       <add name="HttpGet"/> 
       <add name="Documentation"/> 
       <add name="HttpPostLocalhost"/> 
     </protocols> 
    </webServices> 
    ... 
</system.web> 

請參閱http://codeclimber.net.nz/archive/2006/12/22/How-to-enable-an-ASP.NET-WebService-to-listen-to-HTTP.aspx

+0

我只需要使用'json'不能更改它..(受限制) –

+0

您正在使用'type:「POST」 ,'在jquery ajax調用中,所以web服務應該允許'post'請求,你也可以通過編輯'web.config'文件來完成。請參閱http://codeclimber.net.nz/archive/2006/12/22/How-to-enable-an-ASP.NET-WebService-to-listen-to-HTTP.aspx –

+0

嘿,哪個web.config文件是嗎?我的意思是相關的我的網頁或web服務.. –

3

使您的內容類型爲「application/json;字符集= UTF-8" ,如下

$(document).ready(function() { 
$.ajax({ 
type: "POST", 
url: "RSSReader.asmx/GetRSSReader", 
data: "{}", 
contentType: "application/json; charset=utf-8", 
dataType: "json", 
success: function(msg) { 
    // Hide the fake progress indicator graphic. 
    $('#RSSContent').removeClass('loading'); 

    // Insert the returned HTML into the <div>. 
    $('#RSSContent').html(msg.d); 
} 
}); 
}); 

還提到link

+0

感謝它爲我工作,在我的情況下工作在調試模式,但是當我發佈的解決方案刪除方法不起作用,但我已經有內容類型,我只需要添加charset = utf-8 –

1

不同的錯誤,但我有GET方法。

所以我只是改變了類型:「GET」,所以它現在工作。

相關問題