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

编程入门 行业动态 更新时间:2024-10-25 06:21:38
本文介绍了无法从我的html页面将json对象发布到web api。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无法在我的html页面中将json对象发布到web api。 我的api url是 - 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: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- 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: "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: "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:' transapi.ksoftware.co.in/api/CityApi',成功: function (数据){ // 数据是JSON格式的数据 } }); .ajax({ url: 'transapi.ksoftware.co.in/api/CityApi', success: function (data) { // data is JSON formatted data } });

以上即使您的HTML页面没有由服务器端代码备份,也可以应用方法;你可以通过JavaScript的库jQuery来执行它。它会下载您的数据并执行您将在 success 处理程序中编写的操作。 如果您正在使用其他类型的第三方库。该库将公开一个非常有效的函数或类,它将下载此JSON字符串。如果没有,你仍然可以使用.NET framework的 HttpClient [ ^ ]将请求发送到服务器并以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。

本文发布于:2023-10-12 01:23:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   页面   html   json   api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!