AngularJS Mit Asp.net Web-API: $http-post-Rückgabe XMLHttpRequest-Objekt kann nicht geladen werden: Antwort für preflight hat eine ungültige HTTP-Statuscode 405

Wenn Sie versuchen zu buchen, json, Asp.net web-API-server mit $http es ist die Rückkehr der folgende Fehler

XMLHttpRequest cannot load http://localhost:62158/api/video/add.
Response for preflight has invalid HTTP status code 405

aber die gleiche Anfrage $.ajax arbeitet-Datei.

$HTTP-Code

$http.post(url, data, config)
    .success(function (data, status, headers, config) {
        defered.resolve(data);
    })
    .error(function (data, status, header, config) {
        defered.reject(data);
    });

$.ajax-Code

$.ajax({ type: "POST",
    url: url,
    data: newVideo,
    success: function (a) {
         debugger;
    },
    error: function (e) {
       debugger;
    },
    dataType: 'json'
});

asp.net web-api-code und Konfiguration

web.config

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE,     OPTIONS" />
    </customHeaders>
</httpProtocol>

WebApiConfig

public static void Register(HttpConfiguration config)
{
    //Web API configuration and services

    //Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );


    VideoLIbraryDb.Config.setconnection();

    var formatters = GlobalConfiguration.Configuration.Formatters;

    formatters.Remove(formatters.XmlFormatter);

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));


}

API-controller

[RoutePrefix("api/video")]
    public class VideoController : ApiController
    {
        [Route("add")]
        [HttpPost]
        public HttpResponseMessage add([FromBody] db_videos newVideo)
        {
            HttpStatusCode statusCode = HttpStatusCode.NotFound;

            CommonResult result = new CommonResult() ///default
            {
                code = ResultCodes.retry.ToString(),
                message = "Not able to complete request. Retry."
            };

            if (newVideo.video_file_name == null)
                return Request.CreateResponse(statusCode, result);

            if (newVideo.video_file_name.Length < 1)
                return Request.CreateResponse(statusCode, result);


            if (newVideo.insert())
            {
                statusCode = HttpStatusCode.OK;
                result.code = ResultCodes.successfull.ToString();
                result.message = "Video is added";
            }

            return Request.CreateResponse(statusCode, result);
        }
    }
gelten cors auf backend-Seite
keine Ahnung, wie es zu tun. Bitte Anleitung @Rakeschand
es ist ein cors-Problem und muss gelöst werden vom backend asp.net Seite. Sie können die Suche für Sie. Ich weiß nicht, asp sowie.

InformationsquelleAutor Vikas Bansal | 2016-06-04

Schreibe einen Kommentar