本文介绍了WEB API post方法始终具有null json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是我的代码 ================= 客户端 ============ $( document )。ready( function (){ clsdevice = new Object() clsdevice.id = 0; clsdevice.DeviceId = ggg; clsdevice.Longitude = 104.00; clsdevice.Latitude = 125.00 ; var data = JSON .stringify(clsdevice); $ .ajax({ url:' api / Device', // 调用Web API控制器产品 cache: false ,类型:' POST', contentType: application / json; charset = utf-8, data: {'id ' :' + data + '}, dataType: json,成功: function (data){ alert(' 添加'); } }) }); Serverside ========== 使用系统; 使用系统.Collections.Generic; 使用 System.Data; 使用 System.Linq ; 使用 System.Net; 使用 System.Net.Http; 使用 System.Web.Http; 使用 DeviceCollection.Models; 命名空间 DeviceCollection.Controllers { public 类 DeviceController:ApiController { // GET api / HelloWorld public IEnumerable Get() { return new clsDevice [] {}; } // GET api / values / 5 public string 获取( int id) { return null ; } // POST api / values public string 发布([FromBody] string id) { ClsDeviceDL obj = new ClsDeviceDL(); return obj.SaveDevice(id); } ========================================= ===== 使用系统; 使用 System.Collections.Generic; 使用 System.Data; 使用 System.Linq; 使用 System.Web; 使用 System.Web.Script.Serialization; 命名空间 DeviceCollection.Models { public class ClsDeviceDL { public string SaveDevice( string Value) { var jss = new JavaScriptSerializer(); var obj = jss.Deserialize(Value); clsDevice objTest =(clsDevice)obj; string command = @ CALL SP_DeviceMaster( + objTest.id + ,' + objTest.DeviceId + ',' + objTest.Latitude + ', + objTest.Longitude + '); string returnValue = 错误!请尝试再次; DataSet ds = ClsDbTask.GetDataSet(command); if (ds!= null ) { returnValue = 已成功保存; } return returnValue; } } } 字符串id始终为空 please help .... 解决方案 ( document )。ready( function (){ clsdevice = new Object() clsdevice.id = 0; clsdevice.DeviceId = ggg; clsdevice.Longitude = 104.00; clsdevice.Latitude = 125.00; var data = JSON .stringify(clsdevice); .ajax( { url:' api / Device', // 调用Web API控制器产品 cache: false ,类型:' POST', contentType: application / json; charset = utf-8, data: {'id ' :' + data + '}, dataType: json,成功: function (data){ alert(' 添加'); } }) }); Serverside ========== 使用系统; 使用系统.Collections.Generic; 使用 System.Data; 使用 System.Linq ; 使用 System.Net; 使用 System.Net.Http; 使用 System.Web.Http; 使用 DeviceCollection.Models; 命名空间 DeviceCollection.Controllers { public 类 DeviceController:ApiController { // GET api / HelloWorld public IEnumerable Get() { return new clsDevice [] {}; } // GET api / values / 5 public string 获取( int id) { return null ; } // POST api / values public string 发布([FromBody] string id) { ClsDeviceDL obj = new ClsDeviceDL(); return obj.SaveDevice(id); } ========================================= ===== 使用系统; 使用 System.Collections.Generic; 使用 System.Data; 使用 System.Linq; 使用 System.Web; 使用 System.Web.Script.Serialization; 命名空间 DeviceCollection.Models { public class ClsDeviceDL { public string SaveDevice( string Value) { var jss = new JavaScriptSerializer(); var obj = jss.Deserialize(Value); clsDevice objTest =(clsDevice)obj; string command = @ CALL SP_DeviceMaster( + objTest.id + ,' + objTest.DeviceId + ',' + objTest.Latitude + ', + objTest.Longitude + '); string returnValue = 错误!请尝试再次; DataSet ds = ClsDbTask.GetDataSet(command); if (ds!= null ) { returnValue = 已成功保存; } return returnValue; } } } 字符串id始终为空 please帮助.... this is my code=================client side============ $(document).ready(function () { clsdevice=new Object() clsdevice.id = "0"; clsdevice.DeviceId = "ggg"; clsdevice.Longitude = "104.00"; clsdevice.Latitude = "125.00"; var data = JSON.stringify(clsdevice); $.ajax({ url: 'api/Device', //calling Web API controller product cache: false, type: 'POST', contentType: "application/json; charset=utf-8", data: "{'id':'" + data + "'}", dataType: "json", success: function (data) { alert('added'); } }) });Serverside==========using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using DeviceCollection.Models;namespace DeviceCollection.Controllers{public class DeviceController : ApiController{// GET api/HelloWorldpublic IEnumerable Get(){return new clsDevice[] { };} // GET api/values/5public string Get(int id){return null;} // POST api/values public string Post([FromBody] string id){ClsDeviceDL obj = new ClsDeviceDL();return obj.SaveDevice(id);}==============================================using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;using System.Web.Script.Serialization; namespace DeviceCollection.Models{public class ClsDeviceDL{public string SaveDevice(string Value){var jss = new JavaScriptSerializer();var obj = jss.Deserialize(Value);clsDevice objTest = (clsDevice)obj;string command = @"CALL SP_DeviceMaster( " + objTest.id+ ",'" + objTest.DeviceId+ "','" + objTest.Latitude + "'," + objTest.Longitude + "')";string returnValue = "Error! Please try again";DataSet ds = ClsDbTask.GetDataSet(command);if (ds != null){returnValue="saved Successfully";}return returnValue; }}}string id gets null all the time please help.... 解决方案 (document).ready(function () { clsdevice=new Object() clsdevice.id = "0"; clsdevice.DeviceId = "ggg"; clsdevice.Longitude = "104.00"; clsdevice.Latitude = "125.00"; var data = JSON.stringify(clsdevice);.ajax({ url: 'api/Device', //calling Web API controller product cache: false, type: 'POST', contentType: "application/json; charset=utf-8", data: "{'id':'" + data + "'}", dataType: "json", success: function (data) { alert('added'); } }) });Serverside==========using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using DeviceCollection.Models;namespace DeviceCollection.Controllers{public class DeviceController : ApiController{// GET api/HelloWorldpublic IEnumerable Get(){return new clsDevice[] { };} // GET api/values/5public string Get(int id){return null;} // POST api/values public string Post([FromBody] string id){ClsDeviceDL obj = new ClsDeviceDL();return obj.SaveDevice(id);}==============================================using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Web;using System.Web.Script.Serialization; namespace DeviceCollection.Models{public class ClsDeviceDL{public string SaveDevice(string Value){var jss = new JavaScriptSerializer();var obj = jss.Deserialize(Value);clsDevice objTest = (clsDevice)obj;string command = @"CALL SP_DeviceMaster( " + objTest.id+ ",'" + objTest.DeviceId+ "','" + objTest.Latitude + "'," + objTest.Longitude + "')";string returnValue = "Error! Please try again";DataSet ds = ClsDbTask.GetDataSet(command);if (ds != null){returnValue="saved Successfully";}return returnValue; }}}string id gets null all the time please help.... 这篇关于WEB API post方法始终具有null json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-21 23:35