简体   繁体   中英

XMLHttpRequest cannot load URL. Invalid HTTP status code 400

I have WebApi Application and one MVC5 Applicatiion.I am sending request to webApi using angularJS from MVC5 Application.But it is not working fine when I send DELETE or POST request. I am getting 'XMLHttpRequest cannot load URL. Invalid HTTP status code 400' error in Browser.But it is working fine for GET request.See the below code.

Sevice call

  $http.delete("http://localhost:8643/api/values/1");

WebApi

 // DELETE api/values/5

    [HttpDelete]
    public void Delete(int id)
    {
        var emp = employees.FirstOrDefault(x => x.ID == id);

        if(emp!=null)
        {
            employees.Remove(emp);
        }
    }

Even I have enabled Cors in my WebApi. Can anyone please help me.

For the post try to add $.param to your object, and specify the content type. Something like that.

 $http({ method: "POST", url: "http://localhost:8643/api/values", data: $.param({'Type': 'Type', 'Name': 'Name'}), headers: {'Content-Type': 'application/x-www-form-urlencoded',} }) 

Hope this might help. As for Delete and Put, I am still having problems with them myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM