本文介绍了无法从视图中访问web api方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 < html > < head > < script type = text / javascript src = 〜/ Scripts / jquery-1.10.2.js > < / script > < script type = text / javascript > $( document )。ready( function (){ $( #btnAdd )。click( function (){ var PersonalDetails = { id:$( #ID)。val(), name:$( #Name) .val(), adres:$( #Adress)。val(),} $ .ajax ({类型: POST, url: http:// localhost:54516 / api / Values / PostPersonalDetails', data: JSON .stringify(PersonalDetails), contentType: application / json; charset = utf-8, processData: true ,成功: function (data,status,xhr){ alert( 结果是: + status); },错误: function (xhr){ alert(xhr.responseText); } }); }); }); < / script > < / head > < body > < h2 > 创建< / h2 > < div > < 标签 > Id < / label > @ Html.TextBox(ID) < / div > < div > < label > 名称< / label > @ Html.TextBox( 名称) < / div > < div > < label > 年龄< / label > @ Html.TextBox(地址) < / div > < div > < 按钮 id = btnAdd > 添加< / button > < / div > < / body > < / html > API控制器代码 public class ValuesController1:ApiController { [HttpPost] public IHttpActionResult PostPersonalDetails([FromBody] PersonalDetails personaldetails) { if(!ModelState.IsValid) {返回BadRequest(ModelState); } SqlConnection co = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings [con]。ConnectionString); co.Open(); SqlCommand cmd = new SqlCommand(insert into udata values('+ personaldetails.id +','+ personaldetails.name +','+ personaldetails.adres +'),co); cmd.ExecuteNonQuery(); co.Close(); 返回Ok(personaldetails); } } 我尝试了什么: i二手网址:'/ api / Values / PostPersonalDetails' 解决方案 ( document )。ready( function (){ ( #btnAdd)。click( function (){ var PersonalDetails = { id: ( #ID)。val(), 名: <html><head> <script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#btnAdd").click(function () { var PersonalDetails = { "id": $("#ID").val(), "name": $("#Name").val(), "adres": $("#Adress").val(), } $.ajax({ type: "POST", url: 'http://localhost:54516/api/Values/PostPersonalDetails', data: JSON.stringify(PersonalDetails), contentType: "application/json;charset=utf-8", processData: true, success: function (data, status, xhr) { alert("The result is : " + status); }, error: function (xhr) { alert(xhr.responseText); } }); }); }); </script></head><body> <h2>Create</h2> <div> <label>Id</label> @Html.TextBox("ID") </div> <div> <label>Name</label> @Html.TextBox("Name") </div> <div> <label>Age</label> @Html.TextBox("Adress") </div> <div> <button id="btnAdd">Add</button> </div></body></html>API CONTROLLER CODEpublic class ValuesController1 : ApiController { [HttpPost] public IHttpActionResult PostPersonalDetails([FromBody]PersonalDetails personaldetails) { if (!ModelState.IsValid) { return BadRequest(ModelState); } SqlConnection co = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["con"].ConnectionString); co.Open(); SqlCommand cmd = new SqlCommand("insert into udata values('" +personaldetails.id + "','" +personaldetails.name+ "','" +personaldetails.adres+ "')", co); cmd.ExecuteNonQuery(); co.Close(); return Ok(personaldetails); } }What I have tried:i used url:'/api/Values/PostPersonalDetails' 解决方案 (document).ready(function(){("#btnAdd").click(function () { var PersonalDetails = { "id":("#ID").val(), "name": 这篇关于无法从视图中访问web api方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-21 18:05