Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple parameters to my 'POST' ajax call to my webAPI controller > http 404 not found

I can successfully do an ajax call to my webAPI like this:

Javascript:

    $.ajax({
        type: "GET",
        url: "api/Breeze/DeleteMaterials",
        cache: false,
        data: { aa: 'aa', bb: 'bb' },
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        traditional: true,
        success: () => { result.resolve(true); },
        error: (error) => result.reject(error)
    });

Web API:

    [System.Web.Http.HttpGet]
    public void DeleteMaterials(string aa, string bb)
    {
        // doing something here
    }

It works pretty well. The webAPI is reached and the value parameters are present. Now I don't want to keep a 'GET' call because here I'm performing a delete server side, I need wether a 'DELETE' or 'POST'.

I know the 'DELETE' call is not possible because it only accepts 1 parameter and for my case I need to pass 2 parameters.

So I try to replace 'GET' by 'POST' in my previous code and the webAPI is never reached and I get the HTTP error 404: not found.

I already searched a lot in all SO suggestions but none of them gives me what I need.

Any idea?

Thanks.

like image 986
Bronzato Avatar asked Dec 21 '13 11:12

Bronzato


People also ask

How can I pass multiple values in ajax URL?

send multiple data using ajax $. ajax({ url: "/pakainfo_api", type: "GET", data: {p1: "value1", p2: "value2"}, // multiple data we want to send success: function(data){ console. log(data); } }). done(function(){ console.

Can we call the controller of Web API from an ajax call?

The View will be used for calling the Web API 2 Controller's method using jQuery AJAX. The Controller consists of an empty Action method which simply returns the View. Next step is to add an Empty View without Model for the Controller. The View consists of an HTML TextBox element and a Button.

Can we use HTTP GET or POST for ajax calls?

GET vs POST in AJAX callsUnless you are sending sensitive data to the server or calling scripts which are processing data on the server it is more common to use GET for AJAX calls. This is because when using XMLHttpRequest browsers implement POST as a two-step process (sending the headers first and then the data).


2 Answers

Javascript

$.ajax({

    type: "POST",
    url: "api/Breeze/DeleteMaterials",
    cache: false,
    data: JSON.stringify({ aa: 'aa', bb: 'bb' }),
    contentType: "application/json; charset=utf-8",
    traditional: true,
    success: () => { result.resolve(true); },
    error: (error) => result.reject(error)
});

Web API

[ActionName("DeleteMaterials")]
[HttpPost]        
public void DeleteMaterials(JObject jsonData)
{
   dynamic json = jsonData;
   string aa= json.aa;
   string bb= json.bb;
}
like image 163
Zarko Avatar answered Nov 15 '22 05:11

Zarko


Javascript:

$.ajax({
    type: "GET",
    url: "api/Breeze/DeleteMaterials",
    cache: false,
    data: JSON.stringify({ aa: 'aa', bb: 'bb' }),
    contentType: "application/json; charset=utf-8",
    traditional: true,
    success: () => { result.resolve(true); },
    error: (error) => result.reject(error)
});

webAPI:

    public class MyViewModel
    {
        public string Aa { get; set; }
        public string Bb { get; set; }
    }

    [System.Web.Http.HttpPost]
    public void DeleteMaterials(MyViewModel model) 
    {
        // doing some stuffs here
    }
like image 42
Bronzato Avatar answered Nov 15 '22 04:11

Bronzato