Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can't post json object to web api in my html page.

My api url is-
http://www.transapi.ksoftware.co.in/api/RoleApi

my post method in api controller is-
public int Post(RoleModel model)
{
return _repository.InsertRole(model);
}

My javascript function in html page is-

<script type="text/javascript">
function SaveRole() {
var RoleModel = new Object();
RoleModel.RoleName = "test";
RoleModel.Status= true;

$.ajax({
type: "POST",
url: "http://www.transapi.ksoftware.co.in/api/RoleApi",
data: JSON.stringify(RoleModel),

contentType: "application/json",

dataType: "json",
success: function (data, textStatus, xhr) {

alert('success');
},
error: function (jqXHR, textStatus, errorThrown) {
alert('Err');
}
});
}

i call this SaveRole function when page is submitted.
but my problem is i'cant call the post method of api controller and post json object from my html page.
Posted
Updated 13-May-15 20:26pm
v5
Comments
Sergey Alexandrovich Kryukov 6-May-15 11:27am    
This is nothing but usual JSON. What's the problem?
—SA
prthghosh999 13-May-15 2:12am    
i get all data from my api but my problem is whenever i want to call post method to insert or update into the database it returns an error.
my url is http://transapi.ksoftware.co.in/api and passes a model (named RoleModel and its object are RoleName, Status) object into that post method. but it does not call to that method
prthghosh999 13-May-15 2:27am    
i've created those api controller in c#. is there anything that i've to change in my solution.
Sergey Alexandrovich Kryukov 13-May-15 2:31am    
Not clear what you are trying to do.
—SA
Sergey Alexandrovich Kryukov 13-May-15 2:30am    
You did not explain the role of that JSON file. Why?
—SA

1 solution

There are quite a multiple options that you can use to get the JSON data from this API URL. You can use ajax requests to download the data from the API. The following code would depict a scenario,

JavaScript
$.ajax({
   url: 'http://transapi.ksoftware.co.in/api/CityApi',
   success: function (data) { 
      // data is JSON formatted data
   }
});


The above method can be applied even if your HTML page is not backed up by a server-side code; you can execute it through JavaScript's library jQuery. It would download your data and perform the actions that you will write in the success handler.

If you are using some other sort of third-party library. That library would expose a very efficient function or class that would download this JSON string. If there is none, you can still use .NET framework's HttpClient[^] to send requests to server and download the data in JSON format and use libraries like Newtonsoft.Json[^] and deserialize the data.
 
Share this answer
 
v2
Comments
prthghosh999 13-May-15 1:57am    
thank you for your suggestion. actually the problem was in my WebApiConfig file. that does not return json format.
Sergey Alexandrovich Kryukov 13-May-15 3:27am    
What this statement is supposed to mean?
Oh, I guess I can see some problem...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900