在JS和C中通过POST请求解析JSON数据#
问题描述:
我正在使用C#作为我的后端,并且希望向我的C#后端发出JSON对象/字符串请求。在JS和C中通过POST请求解析JSON数据#
我有C#代码如下所示(GET请求):
[WebMethod]
[ScriptMethod(UseHttpGet = true,
ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public static string GetOptionsServiceHttpGet(string variableId)
{
// Do database stuff
string retval = Serialize stuff/function goes here
return retval;
}
JavaScript的前端代码是这样的:
function Variable_Proxy() { }
Variable_Proxy.GetVarOptionsHttpGet =
function (variableId, successCallback, failureCallback) {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "file.aspx/GetOptionsServiceHttpGet?varId=\"" + variableId + "\",
success: function (data) { successCallback(data); },
error: function (data) { failureCallback(data); }
});
}
我如何做一个帖子或任何让我的JSON点击我的后端对象?
答
例如,如果你要发送的数据是一个简单的雇员对象可以做一个AJAX POST如下:
C#后端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for TestWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class TestWebService : System.Web.Services.WebService {
public TestWebService() {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string GetPersonData(int id, Person objPerson) {
return "You have submitted data with ID: " + id.ToString() + " Name: " + objPerson.Name + " and Email: " + objPerson.Email;
}
[WebMethod]
public Employee CreateEmployee(int id, Person objPerson) {
Employee objEmployee = new Employee();
objEmployee.ID = id;
objEmployee.Name = objPerson.Name;
objEmployee.Email = objPerson.Email;
return objEmployee;
}
public class Person{
public string Name {get;set;}
public string Email {get;set;}
}
public class Employee : Person {
public int ID { get; set; }
}
}
jQuery的前端:
$("#btnSubmit").click(function() {
// create the json string
var jsonData = JSON.stringify({
'id': $("#txtID").val(),
objPerson: {
'name': $("#txtName").val(),
'email': $("#txtEmail").val()
}
});
$.ajax({
type: "POST",
url: "/TestWebService.asmx/CreateEmployee",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (xhr, status, error) {
//alert the error if needed
$("#result").html("Sorry there is an error: " xhr.responseText);
},
success: function (responseData) {
// show the response data from webservice. Note: the d represent the object property data passed by webservice. It can an object of properties or just single property
$("#result").html("The id is " + responseData.d.ID + " And Name is " + responseData.d.Name + " And Email is " + responseData.d.Email);
}
});
});
答
try
{
List<DataObject> DataList = new List<DataObject>();
...................
return new { Result = "OK", Records = DataList };
} catch (Exception ex) {
return new { Result = "ERROR", Message = ex.Message };
}
酷什么母鹿s的C#看起来像(+1的帮助) – cdub
没关系,很好的回答thx – cdub
我已经更新了我的答案 – chridam