本文介绍了无法从我的html页面将json对象发布到web api。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 无法在我的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/RoleApimy 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.推荐答案 以上即使您的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。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 18:16