2016-06-09 98 views
0

我得到這個錯誤:響應具有無效的HTTP狀態碼405個AngularJS

的XMLHttpRequest無法加載http://apidev.facu.la/api/json/infotag/search/?criteria=&infotagType=book&companyid=2。當嘗試執行此httpget調用時,預檢反應的HTTP狀態碼405無效:

$http({ 
       method: 'GET', 
       dataType: "json", 
       url: 'http://apidev.facu.la/api/json/infotag/search/?criteria=&infotagType=book&companyid=2', 
       headers: { 
        "Content-Type": "application/json" 
       } 
      }).then(function successCallback(response) { 
       alert('ok'); 
      }, function errorCallback(response) { 
       alert('error'); 
       console.log(response); 
      }); 

任何想法我錯過了什麼? 郵遞員電話工作得很好。 感謝

UPDATE:

端點代碼:

[WebInvoke(Method = "GET", 
      RequestFormat = WebMessageFormat.Json, 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Bare, 
      UriTemplate = "/json/infotag/search/?criteria={criteria}&infotagType={infotagType}&companyid={companyid}")] 
     public XElement InfoTagSearchXML_json(string criteria, string infotagType, int companyid) 
     { 
      return InfoTagSearchXML(criteria, infotagType, companyid); 
     } 
+2

法(狀態代碼405)意味着您沒有在您的端點上定義任何OPTIONS方法。 CORS請求是強制性的。 請參閱[CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) 您可以提供有關您的端點的任何詳細信息嗎? (語言,框架等) – KRONWALLED

+0

@Kronwalled只是將問題添加到問題主體。 Thx幫助我! – Laziale

回答

0

您可以爲方法選項添加WebInvoke到您的ServiceContract這樣的:不準

[ServiceContract] 
public interface ITestService 
{ 
    [WebInvoke(Method = "OPTIONS", UriTemplate = "*")] 
    void getOptions(); 

    ... other invokes 
} 

public class Service : ITestService 
{ 
    public void getOptions() 
    { 
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); 
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, OPTIONS"); 
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type"); 
    } 

    ... other invokes 
} 
+0

謝謝@Kronwalled。一旦我補充一點,我如何將它用於我的電話?或者那是我唯一需要做的事情? – Laziale

+0

當您啓動GET請求時,應自動調用它 – KRONWALLED

+0

更改後獲取此錯誤https://i.gyazo.com/83267cb7a48c2a1167c015f83b160079.png – Laziale

相關問題