Hi, I will ask my question with clear example. Please follow the below steps.
I have created the following web api action within some api controller.
[HttpPost]
public HttpResponseMessage SubmitOnServer(RequestObject request)
{
HttpResponseMessage result = null;
// I have written logic to save data on server and return back the result as response object.
ResponseObject response=SaveOnServer(request);
//Now I want to send response object to Client. Here client is either javascript or C#.Net.
result = Request.CreateResponse(HttpStatusCode.OK, response);
return result;
}
I can call this web api action from C#.Net client by using HttpClient object like the following
But, my question is: How to call the above web api action from javascript by using JQuery.ajax().
Actually I have requirement like call web api actions from both javascript and C#.Net clients.
Please suggest best way for this?
Note: With some modification I can call it is from javascript but not from C#.Net. You can see the modified code below
[HttpPost]
public System.Web.Mvc.JsonResult SubmitOnServer(RequestObject request)
{
System.Web.Mvc.JsonResult result =new System.Web.Mvc.JsonResult();
// I have written logic to save data on server and return back the result as response object.
ResponseObject response=SaveOnServer(request);
//Now I want to send response object to Client. Here client is either javascript or C#.Net.
result.Data = response;
return result;
}
Make sure to add the Accept application/json header from ajax
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
RajeshKalis
Member
27 Points
54 Posts
Calling WebAPI actions from both Javascript and C#.Net clients
Nov 20, 2012 12:16 PM|LINK
Hi, I will ask my question with clear example. Please follow the below steps.
I have created the following web api action within some api controller.
[HttpPost] public HttpResponseMessage SubmitOnServer(RequestObject request) { HttpResponseMessage result = null; // I have written logic to save data on server and return back the result as response object. ResponseObject response=SaveOnServer(request); //Now I want to send response object to Client. Here client is either javascript or C#.Net. result = Request.CreateResponse(HttpStatusCode.OK, response); return result; }I can call this web api action from C#.Net client by using HttpClient object like the following
public ResponseObject SubmitOnServer(RequestObject request) { try { var resp = Client.PostAsJsonAsync(webApiUrl, request).Result; if (resp.StatusCode == HttpStatusCode.OK) { return resp.Content.ReadAsAsync<ResponseObject>().Result; } return null; } catch (Exception ex) { return null; } }But, my question is: How to call the above web api action from javascript by using JQuery.ajax().
Actually I have requirement like call web api actions from both javascript and C#.Net clients.
Please suggest best way for this?
Note: With some modification I can call it is from javascript but not from C#.Net.
You can see the modified code below
[HttpPost] public System.Web.Mvc.JsonResult SubmitOnServer(RequestObject request) { System.Web.Mvc.JsonResult result =new System.Web.Mvc.JsonResult(); // I have written logic to save data on server and return back the result as response object. ResponseObject response=SaveOnServer(request); //Now I want to send response object to Client. Here client is either javascript or C#.Net. result.Data = response; return result; }K. Rajesh.
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: Calling WebAPI actions from both Javascript and C#.Net clients
Nov 20, 2012 04:20 PM|LINK
Make sure to add the Accept application/json header from ajax
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
RajeshKalis
Member
27 Points
54 Posts
Re: Calling WebAPI actions from both Javascript and C#.Net clients
Nov 21, 2012 03:39 AM|LINK
Can you provide a sample code snippet to add accept application/json header.
K. Rajesh.
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: Calling WebAPI actions from both Javascript and C#.Net clients
Nov 21, 2012 04:33 AM|LINK
A simple ajax request may be,
$.ajax({
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
url: "/api/Blah",
dataType: "json",
success: function(json){
//do something...
}
});
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD