无法从我的html页面将json对象发布到web api。 [英] Can't post json object to web api from my html page.

查看:75
本文介绍了无法从我的html页面将json对象发布到web api。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在我的html页面中将json对象发布到web api。



我的api url是 -

http://www.transapi.ksoftware.co.in/api/RoleApi



api控制器中的post方法是 -

public int Post(RoleModel model)

{

return _repository.InsertRole(model);

}



我在html页面的javascript函数是 -



< script type =text / javascript>

函数SaveRole(){

var RoleModel = new Object();

RoleModel.RoleName =" test";

RoleModel.Status = true;



$ .ajax ({

类型:POST,

url:http://www.transapi.ksoftware.co.in/api/RoleApi,

数据:JSON.stringify(RoleModel),



contentType:application / json,



dataType:json,

成功:函数(data,textStatus,xhr){



alert('success') ;

},

错误:函数(jqXHR,textStatus,errorThrown) {

alert('Err');

}

});

}



i在提交页面时调用此SaveRole函数。

但我的问题是我不能调用api控制器的post方法并从我的html页面发布json对象。

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.

推荐答案

.ajax({

类型:POST,

url:http:/ /www.transapi.ksoftware.co.in/api/RoleApi,

数据:JSON.stringify(RoleModel),



contentType :application / json,



dataType:json,

成功:函数(data,textStatus,xhr){



alert('success');

},

错误:function(jqXHR,textStatus,errorThrown){

提醒('错误');

}

});

}



i在提交页面时调用此SaveRole函数。

但我的问题是我不能调用api控制器的post方法并从我的html页面发布json对象。
.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.


您可以使用多个选项从此API URL获取JSON数据。您可以使用ajax请求从API下载数据。以下代码将描述一个场景,



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,


.ajax({
url:' http://transapi.ksoftware.co.in/api/CityApi'
成功: function (数据){
// 数据是JSON格式的数据
}
});
.ajax({ url: 'http://transapi.ksoftware.co.in/api/CityApi', success: function (data) { // data is JSON formatted data } });





以上即使您的HTML页面没有由服务器端代码备份,也可以应用方法;你可以通过JavaScript的库jQuery来执行它。它会下载您的数据并执行您将在 success 处理程序中编写的操作。



如果您正在使用其他类型的第三方库。该库将公开一个非常有效的函数或类,它将下载此JSON字符串。如果没有,你仍然可以使用.NET framework的 HttpClient [ ^ ]将请求发送到服务器并以JSON格式下载数据并使用像< a href =https://www.nuget.org/packages/newtonsoft.json/> Newtonsoft.Json [ ^ ]并反序列化数据。



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.


这篇关于无法从我的html页面将json对象发布到web api。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆