jQuery 带参数传递的 Json ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6534510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Json ajax with parameter passing
提问by sly_Chandan
function BindJson() {
$.ajax({
type: "POST",
url: "NewPage.aspx/SerializeJson",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function (data1) {
alert(data1);
}
})
}
[WebMethod]
public static string SerializeJson()
{
JavaScriptSerializer js = new JavaScriptSerializer();
//Person p2 = js.Deserialize<Person>(str);
return "";
}
How do I pass parameters as data to my serializeJson function?
如何将参数作为数据传递给我的 serializeJson 函数?
回答by NakedBrunch
This will work for you (full working code sample below). The key is to pass in a Person object. Also, I used a simple web service (myService.asmx) instead of an aspx page. Why bother with the extra overhead if it isn't needed?
这对您有用(下面的完整工作代码示例)。关键是传入一个 Person 对象。此外,我使用了一个简单的 Web 服务 (myService.asmx) 而不是 aspx 页面。如果不需要,为什么还要为额外的开销而烦恼呢?
The key is, on the client, create a Person object and then use JSON.stringifyto pass the Person object to the webservice.
关键是,在客户端创建一个 Person 对象,然后使用JSON.stringify将 Person 对象传递给 Web 服务。
Javascript
Javascript
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"></script>
<script type="text/javascript">
function BindJson() {
$.ajax({
type: "POST",
url: "myService.asmx/SerializeJson",
data: JSON.stringify({ person:{ firstName: "Denny", lastName: "Cherian", department: "Microsoft PSS", address: { addressline1: "Microsoft India GTSC", addressline2: "PSS - DSI", city: "Bangalore", state: "Karnataka", country: "India", pin: "560028" }, technologies: ["IIS", "ASP.NET", "JavaScript", "AJAX"] }}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data1) {
alert(data1.d);
},
error: function (request, status, errorThrown) {
alert(status);
}
});
}
$(document).ready(function() {
BindJson();
});
</script>
C#
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace TestProject
{
/// <summary>
/// Summary description for myService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 myService : System.Web.Services.WebService
{
[WebMethod]
public string SerializeJson(Person person)
{
return "Success";
}
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public string department { get; set; }
public Address address { get; set; }
public string[] technologies { get; set; }
}
public class Address
{
public string addressline1 { get; set; }
public string addressline2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string country { get; set; }
public string pin { get; set; }
}
}
}
回答by Lamps
You can use Serialize()method that jQuery offers to pass the data.
您可以使用jQuery 提供的Serialize()方法来传递数据。
Please see this article
请看这篇文章