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.
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.
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.
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).
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;
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With