2017-03-12 4 views
0

Ich habe ein Problem, wenn ich AngularJS Daten zum Web API Endpunkt poste. Vom Client-Browser erhalte ich:Ajax Post zu Web API (405 Methode nicht erlaubt)

405 (Method Not Allowed) 
Response for preflight has invalid HTTP status code 405 

Ich habe zwei separate Projekte, die beide in localhost ausgeführt werden. Auf meiner Web-API habe ich EnableCors() auf Config gesetzt.

Wenn ich gesetzt Content-Typ des Headers an:

'Content-type': 'application/x-www-form-urlencoded; charset=utf-8' 

Dann ist mein Web-API-Endpunkt traf es in der Lage zu. Mein Objektargument ist jedoch null. Könnte das XML-Format und nicht JSON sein? Wie gehe ich vor, um das zu lösen?

Client-Seite Code:

function signUp(data) { 
     $http({ 
      method: 'POST', 
      url: 'http://localhost:15218/api/account/register', 
      data: JSON.stringify(data), 
      headers: { 
       'Content-type': 'application/json' 
      } 

    }).then(function successCallback(response) { 
      console.log(response); 
     }, function errorCallback(response) { 
      console.log(response); 
     }); 
    } 
} 

Server Side Methode Signatur:

[HttpPost] 
    [Route("Register")] 
    public async Task<HttpResponseMessage> Register(UserCommand command) 
+0

Sie müssen cors auf der Serverseite aktivieren – Sajeetharan

+0

Ja, ich habe und immer noch nicht funktioniert –

+0

scheint es nicht angewendet – Sajeetharan

Antwort

0

Sie cors auf den Dienst

durch Hinzufügen Datei global.asax
protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
    { 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE"); 

     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
     HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); 
     HttpContext.Current.Response.End(); 
    } 
} 
+0

Ich habe versucht, den Header Inhaltstyp zu Anwendung/x-www-Form-urlencoded; charset = utf-8, aber wenn es meine API trifft, ist das Parameterergebnis null. Könnte das sein, weil die Einstellung auf ein anderes Format, d. H. XML statt JSON, eingestellt ist? –

+0

sollte es json sein – Sajeetharan

+0

Ich habe auch einen einfachen Test gemacht, indem ich nur ein int über und auf dem Web-API gesendet habe ich explizit [FromBody] aber 0 anstelle der tatsächlichen Zahl, die ich auf der Client-Seite gesetzt. –

0

Response for preflight has invalid HTTP status code 405

aktivieren

Bevor Sie die POST-Anfrage machen, die Sie vornehmen möchten, macht der Browser eine OPTIONS-Anfrage, in der er um Erlaubnis bittet.

Der Server antwortet auf die OPTIONS-Anfrage ohne 200 Antwort. Die Fehlermeldung sagt das explizit. (Vielleicht Access-Control-Allow-Origin Ursache für das Problem)

Bevor Sie etwas anderes, müssen Sie für Art der Anfrage Methode überprüfen. Wenn es OPTIONS ist, übergeben Sie 200 Antwortcode.

+0

Ich habe versucht, den Inhaltstyp des Headers in application/x-www-form-urlencoded zu ändern; charset = utf-8, aber wenn es meine API trifft, ist das Parameterergebnis null. Könnte das sein, weil die Einstellung auf ein anderes Format, d. H. XML statt JSON, eingestellt ist? –

Verwandte Themen