如何能将json字符串和一些其他参数通过一个ajax传到后台? 如何使用ajax将json传入后台数据
\u5982\u4f55\u80fd\u5c06json\u5b57\u7b26\u4e32\u548c\u4e00\u4e9b\u5176\u4ed6\u53c2\u6570\u901a\u8fc7\u4e00\u4e2aajax\u4f20\u5230\u540e\u53f0\uff1f\u53ef\u4ee5\u5c06\u591a\u4e2a\u540cname\u5c5e\u6027\u63a7\u4ef6\u7684\u503c\u7ec4\u5408\u6210\u4e00\u4e2ajson\u6570\u7ec4\u6216\u6570\u7ec4\u5b57\u7b26\u4e32\uff0c\u4f20\u5230java\u540e\u53f0\u540e\u518d\u5e8f\u5217\u5316\u6210json\u6570\u7ec4\uff0c\u53d6\u51fa\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u6d4b\u8bd5\u4ee3\u7801\uff1a
$("#submit").click(function () {
//\u5c06\u591a\u4e2a\u540cname\u7684\u503c\u7ec4\u6210\u4e00\u4e2ajson\u6570\u7ec4\u5b57\u7b26\u4e32
var jsondata = "[";
$("[name=test]").each(function () {
jsondata += '{"value":"' + this.value + '"},';
});
//\u53bb\u6389\u6700\u540e\u4e00\u4e2a","
var reg = /,$/gi;
jsondata = jsondata.replace(reg, "");
jsondata += "]";
alert(jsondata);
//ajax\u63d0\u4ea4\u6570\u636e
$.post("test.action", { "test": jsondata }, function (result) { alert(result) });
});
\u9996\u5148\u91c7\u7528jquery\u5185\u90e8\u5c01\u88c5\u597d\u7684\u65b9\u6cd5\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u6211\u4eec\u53ea\u9700\u505a\u7684\u5c31\u662f\u4fee\u6539\u91cc\u9762\u7684\u4e00\u4e9b\u914d\u7f6e\uff1a
\u4ee5\u4e0b\u4ee3\u7801\u662f\u5bf9$.ajax()\u7684\u89e3\u6790\uff1a
$.ajax({
type: "POST", //\u63d0\u4ea4\u65b9\u5f0f
contentType: "application/json; charset=utf-8", //\u5185\u5bb9\u7c7b\u578b
dataType: "json", //\u7c7b\u578b
url: "\u524d\u53f0\u5730\u5740/\u540e\u53f0\u65b9\u6cd5", //\u63d0\u4ea4\u7684\u9875\u9762\uff0c\u65b9\u6cd5\u540d
data: "parameter", //\u53c2\u6570\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u4e3anull
success: function (data) { //\u5982\u679c\u6267\u884c\u6210\u529f\uff0c\u90a3\u4e48\u6267\u884c\u6b64\u65b9\u6cd5
alert(data.d); //\u7528data.d\u6765\u83b7\u53d6\u540e\u53f0\u4f20\u8fc7\u6765\u7684json\u8bed\u53e5\uff0c\u6216\u8005\u662f\u5355\u7eaf\u7684\u8bed\u53e5
},
error: function (err) { //\u5982\u679c\u6267\u884c\u4e0d\u6210\u529f\uff0c\u90a3\u4e48\u6267\u884c\u6b64\u65b9\u6cd5
alert("err:" + err);
}
});
\u5f53\u7136\u6211\u521d\u6b21\u5b66\u4e60\u7684\u65f6\u5019\uff0c\u770b\u5230\u8fd9\u4e9b\u4e5f\u662f\u6709\u4e9b\u832b\u7136\u7684\uff0c\u56e0\u4e3a\u4e0d\u77e5\u9053\u5230\u5982\u4f55\u624d\u80fd\u5c06\u5176\u7528\u5230\u81ea\u5df1\u7684\u7a0b\u5e8f\u91cc\u9762\uff0c\u6240\u4ee5\u5c31\u5199\u4e86\u4e00\u4e2a\u5c0f\u7684\u68c0\u6d4b\u7f51\u9875\u6765\u6d4b\u8bd5\u4e00\u4e0b\uff0c\u5982\u679c\u4f60\u6d4b\u8bd5\u7684\u65f6\u5019\u6ca1\u6709\u8fbe\u5230\u4f60\u60f3\u8981\u7684\u7ed3\u679c\uff0c\u90a3\u4e48\u5e0c\u671b\u4f60\u56de\u5934\u770b\u4e00\u4e0b\uff0c\u524d\u53f0\u662f\u5426\u5199\u7684\u6709\u95ee\u9898\uff0c\u6216\u8005\u662f\u53c2\u8003\u4e00\u4e0b\u4e0b\u6587\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879\u5427\u3002
\u524d\u53f0\u4ee3\u7801 \uff1a
\u6d4b\u8bd5ajax
function testAjax() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "number3.aspx/GetJson",//\u4f20\u5165\u540e\u53f0\u7684\u5730\u5740/\u65b9\u6cd5
data: "{'RID':'123'}",//\u53c2\u6570\uff0c\u8fd9\u91cc\u662f\u4e00\u4e2ajson\u8bed\u53e5
success: function (data) {
var result = data.d;
alert(result);
},
error: function (err) {
alert("err:" + err);
}
});
}
\u7528button\u6d4b\u8bd5ajax
\u540e\u53f0\u4ee3\u7801\uff1a
using System;
using System.Web.Services;
namespace ajax1
{
public partial class number3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetJson(string RID)
{
return "{'ID':'" + RID + "'}";
}
}
}
\u540e\u53f0\u4ee3\u7801\u4e2d\u4ee5\u9ec4\u8272\u4e3a\u80cc\u666f\u7684\u5c31\u662f\u6211\u4eec\u8981\u6ce8\u610f\u7684\u5730\u65b9\uff1a
1.using System.Web.Services;\u5bf9\u5e94\u4e0b\u9762\u7684[WebMethod]\u8fd9\u4e2a\u4e00\u5b9a\u8981\u52a0\u4e0a\u7684\u3002
2.\u540e\u53f0\u5199\u7684\u65b9\u6cd5\u4e00\u5b9a\u662f\u516c\u5171\u9759\u6001\u7684\u5373\u4e00\u5b9a\u662fpublic static\u5f00\u5934\u7684\u3002
3.\u53c2\u6570\u4e00\u5b9a\u662f\u524d\u53f0\u7684data\u6240\u4f20\u53c2\u6570\u7684\u952e
\u524d\u53f0\u4e2d\u8981\u6ce8\u610f\u7684\u5730\u65b9\uff1a
1.\u5927\u5bb6\u5e94\u8be5\u4f1a\u6ce8\u610f\u5230\uff0c\u524d\u53f0\u6211\u7528\u7684\u662f\u4e24\u4e2abutton\u6765\u6d4b\u8bd5\uff0c\u4f46\u662f\u7b2c\u4e00\u4e2a\u662f\u4e0d\u884c\u7684\uff0c\u9875\u9762\u4f1a\u5237\u65b0\u4e00\u4e0b\uff0c\u5176\u5b9e\u8fd9\u90fd\u662f<form
id="form1"
runat="server">\u8fd9\u884c\u4ee3\u7801\u7684\u95ee\u9898\uff0c\u6807\u7b7e\u4f1a\u63d0\u4ea4\u672c\u9875\u9762\u7684\u5185\u5bb9\uff0c\u4ece\u800c\u5bfc\u81f4\u5f02\u6b65\u5237\u65b0\u5931\u8d25\u3002\u6240\u4ee5\u5efa\u8bae\u5927\u5bb6\u4e0d\u8981\u7528\u6807\u7b7e\u3002\u4f46\u662f\u5982\u679c\u4e0d\u5f97\u4e0d\u7528\u7684\u8bdd\uff0c\u89e3\u51b3\u529e\u6cd5\u8fd8\u662f\u6709\u7684\uff0c\u76ee\u524d\u6211\u77e5\u9053\u7684\u53ea\u6709\u4e24\u4e2a\uff1a
\u3000\u3000\u2460\uff1a\u5c06<form id="form1"
runat="server">\u4ee3\u7801\u53bb\u6389\uff0c\u5f53\u7136\u5982\u679c\u672c\u9875\u9762\u6709\u8981\u63d0\u4ea4\u7684\u5185\u5bb9\u5c31\u4f1a\u5f88\u9ebb\u70e6\u4e86
\u3000\u3000\u2461\uff1a\u5c06<button onclick="aaa();return
false;">\u7528button\u6d4b\u8bd5ajax\u4ee3\u7801\u6539\u4e3a\uff1a<button
onclick="aaa();return false;">\u7528button\u6d4b\u8bd5ajax
2\u3002\u524d\u53f0\u7684\u6d4b\u8bd5\u7ed3\u679c\u662f\uff1a
\u90a3\u4e48\u6211\u4eec\u5982\u4f55\u6765\u53ea\u83b7\u53d6json\u540e\u9762\u7684\u503c\uff0c\u800c\u4e0d\u662f\u6574\u4e2ajson\u8bed\u53e5\u5462\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06json\u8bed\u53e5\u5bf9\u8c61\u5316\uff0c\u7136\u540e\u6839\u636e\u952e\u6765\u53d6\u5f97\u5bf9\u5e94\u7684\u503c\uff1a\u524d\u53f0\u7684testAjax()\u7684\u65b9\u6cd5\u6539\u4e3a\uff1a
function testAjax() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "number3.aspx/GetJson",
data: "{'RID':'123'}",
success: function (data) {
var result = eval("(" + data.d + ")");//\u8fd9\u53e5\u8bdd\u662f\u5c06json\u8bed\u53e5\u5bf9\u8c61\u5316
alert(result.ID);
},
error: function (err) {
alert("err:" + err);
}
});
}
\u6b64\u65f6\u7ed3\u679c\u4e3a\uff1a
\u4ee5\u4e0a\u5c31\u662f\u521d\u6b65\u5b66\u4e60json\u5f53\u65f6\u9047\u5230\u7684\u95ee\u9898\u6240\u7559\u4e0b\u7684\u7ecf\u9a8c\u3002\u540c\u65f6\u6211\u60f3\u9a8c\u8bc1\u4e00\u4e0b\u662f\u5426\u80fd\u591f\u5728\u540e\u53f0\u91cd\u8f7d\u65b9\u6cd5\u6765\u5b9e\u73b0\u6839\u636e\u524d\u53f0\u7684data\u662f\u5426\u6709\u53c2\u6570\u6765\u5224\u65ad\u8981\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u6240\u4ee5\u6211\u5c06\u4ee3\u7801\u6539\u52a8\u4e86\u4e00\u4e0b\uff1a
\u6539\u52a8\u540e\u7684\u524d\u53f0\u4ee3\u7801
\u6d4b\u8bd5ajax
function testAjax() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "number3.aspx/GetJson",
data: "{'RID':'123'}",
success: function (data) {
var result = eval("(" + data.d + ")");
alert(result.ID);
},
error: function (err) {
alert("err:" + err);
}
});
}
function aaa() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "number3.aspx/GetJson",
success: function (data) {
alert(data.d);
},
error: function (err) {
alert("err:" + err);
}
});
}
\u7528button\u6d4b\u8bd5ajax
\u6539\u52a8\u540e\u7684\u540e\u53f0\u4ee3\u7801
using System;
using System.Web.Services;
namespace ajax1
{
public partial class number3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetJson()
{
return "hello ajax";
}
[WebMethod]
public static string GetJson(string RID)
{
return "{'ID':'" + RID + "'}";
}
}
}
\u6240\u5f97\u7684\u6548\u679c\u4e3a\uff1a
\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2a\u6309\u94ae\u70b9\u51fb\u540e\u6548\u679c\u4e3a\uff1a
\u7b2c\u4e09\u4e2a\u6309\u94ae\u70b9\u51fb\u540e\u6548\u679c\u4e3a\uff1a
\u6240\u4ee5\u6211\u7684\u521d\u6b65\u7ed3\u8bba\u4e3a\uff1a\u540e\u53f0\u7684\u91cd\u6784\u51fd\u6570\u662f\u4e0d\u6210\u529f\u7684\uff0c\u5982\u679c\u6709\u7684\u91cd\u6784\u53c2\u6570\u7684\u8bdd\uff0c\u53ea\u4f1a\u6267\u884c\u5e26\u53c2\u6570\u7684\uff0c\u800c\u4e0d\u4f1a\u6267\u884c\u90a3\u4e2a\u4e0d\u5e26\u53c2\u6570\u7684\u3002\u73b0\u5728\u8fd8\u4e0d\u662f\u592a\u660e\u767d\u4e3a\u4ec0\u4e48\u4f1a\u8fd9\u6837\uff0c\u6240\u4ee5\u5e0c\u671b\u660e\u767d\u539f\u7406\u7684\u5206\u4eab\u4e00\u4e0b\u81ea\u5df1\u7684\u89c2\u70b9\u3002\u5f53\u7136\u8fd9\u53ea\u662f\u4e2a\u4eba\u89c2\u70b9\uff0c\u5982\u82e5\u6709\u8bef\uff0c\u671b\u8bf7\u6307\u6b63\u3002
$.ajax({
async: false,
type: 'Post',
url: '../Common/GetSYQMessage.ashx',
data: {
'year': encodeURIComponent($("#year").val()),
'DKBH': encodeURIComponent($("#DKBH").val())
},
dataType: 'json',
success: function (response) {
var d = eval(response);
var str = "";
console.log(d);
if (d.result == 's' && d.Number != "") {//获取的数据
$("#hidNumber").val(d.Number);
var BGCS = d.BGCS == "0" ? "无" : d.BGCS+"次";
var dt = eval(d.data);
console.log(dt);
AddHtml(dt,BGCS);
}
else {
if (d.data != "") {
MsgWarn(d.data, '2', '系统信息');
}
}
}
});
1,知道后端接口地址
2,跟后端确认相应的字段名
3,ajax通过json格式发送就ok了。
绛旓細鍙互灏嗗涓悓name灞炴ф帶浠剁殑鍊肩粍鍚堟垚涓涓猨son鏁扮粍鎴栨暟缁勫瓧绗︿覆锛屼紶鍒癹ava鍚庡彴鍚庡啀搴忓垪鍖栨垚json鏁扮粍锛屽彇鍑烘暟鎹銆備互涓嬫槸娴嬭瘯浠g爜锛 ("#submit").click(function () { //灏嗗涓悓name鐨勫肩粍鎴愪竴涓猨son鏁扮粍瀛楃涓 var
绛旓細渚嬪瓙锛歶rl鍐欒嚜宸辩殑绔彛鎴栬呭鐞嗙▼搴忕瓑锛宒ata閲屾寜鐓ф牸寮忓啓鎯宠浼犵殑鍙傛暟锛宔ncodeURIComponent鏄姞瀵嗭紝涓鑸兘浼氬姞瀵嗗悗鍐嶄紶锛屼笉鐒舵湁娉勯湶涓汉淇℃伅鐨勯闄╋紝 dataType: 'json',纭畾鏍煎紡涓簀son鏍煎紡锛宔val 灏嗗悗鍙板洖浼犵殑string瀛楃杞浆鎹㈡垚json瀵硅薄锛屽湪success閲屽彇鍑烘斁鍒伴〉闈笂銆傛湜閲囩撼鍝︼紒.ajax({ async: false,type...
绛旓細杩欐浠g爜棣栧厛瀹氫箟浜嗕竴涓寘鍚鏉傜粨鏋勭殑JSON瀛楃涓锛岀劧鍚庝娇鐢╜eval()`鍑芥暟灏嗗叾杞寲涓篔avaScript瀵硅薄銆傛帴鐫锛岄氳繃寰幆閬嶅巻鏁扮粍锛屾垜浠彲浠ヨ闂拰鎿嶄綔鍏朵腑鐨勬瘡涓厓绱犮傚煎緱娉ㄦ剰鐨勬槸锛宍eval()`鍑芥暟鍦ㄥ鐞咼SON鏃跺彲鑳藉瓨鍦ㄥ畨鍏ㄩ闄╋紝鎺ㄨ崘浣跨敤涓撻棬鐨凧SON瑙f瀽鍣ㄨ繘琛屽鐞嗭紝浠ラ伩鍏嶆伓鎰忎唬鐮佹墽琛屻
绛旓細褰撻渶瑕佸皢瀛楃涓鎸夐楀彿鍒嗛殧涓篔SON鏁扮粍锛屽彲浠ヤ娇鐢╜split()`鏂规硶锛歚String[] values = jsonString.split(",");`濡傛灉瀛楃涓叉湰韬凡缁忔槸JSON鏍煎紡锛屼絾闇瑕佹寜閿煎鎷嗗垎锛屽彲浠ュ厛瑙f瀽涓篔SONObject锛岀劧鍚庤幏鍙栧叾閿煎锛歚JSONObject jsonObject = JSON.parseObject(jsonString); String key = jsonObject.getString...
绛旓細娴忚鍣ㄦ敮鎸佺殑杞崲鏂瑰紡娴忚鍣細Javascript鏀寔鐨勮浆鎹㈡柟寮忥細eval((+jsonstr+));//鍙互灏唈son瀛楃涓杞崲鎴恓son瀵硅薄,娉ㄦ剰闇瑕佸湪json瀛楃澶栧寘瑁逛竴瀵瑰皬鎷彿娉細ie8(鍏煎妯″紡),ie7鍜宨e6涔熷彲浠ヤ娇鐢╡val()灏嗗瓧绗︿覆杞负JSON瀵硅薄锛屼絾涓嶆帹鑽愯繖浜涙柟寮忥紝杩欑鏂瑰紡涓嶅畨鍏╡val浼氭墽琛json涓涓殑琛ㄨ揪寮忋侸SON鐨勮浆鎹㈡柟寮忔彁渚...
绛旓細1. JSON.parse鏂规硶绠浠 `JSON.parse`鏄疛avaScript鍐呯疆鐨勪竴涓柟娉曪紝涓撻棬鐢ㄤ簬灏咼SON鏍煎紡鐨勫瓧绗︿覆杞崲涓篔avaScript瀵硅薄銆傝繖涓柟娉曢潪甯搁珮鏁堬紝骞朵笖鍙互甯姪閬垮厤鎵嬪姩瑙f瀽JSON鏃跺彲鑳介亣鍒扮殑閿欒銆2. 浣跨敤鏂规硶 浣跨敤`JSON.parse`鏂规硶闈炲父绠鍗曘傚亣璁炬垜浠湁涓涓JSON瀛楃涓锛屾垜浠彧闇瑕佸皢杩欎釜瀛楃涓蹭綔涓哄弬鏁颁紶閫掔粰`JSON....
绛旓細鏂瑰紡涓锛氫娇鐢╜JSON.stringify`鏂规硶 杩欐槸JavaScript涓父鐢ㄧ殑灏咼SON瀵硅薄杞崲涓瀛楃涓鐨勬柟娉曘傚畠鎺ュ彈涓涓狫SON瀵硅薄浣滀负鍙傛暟锛屽苟杩斿洖涓涓〃绀鸿瀵硅薄鐨勫瓧绗︿覆銆備緥濡傦細`let jsonString = JSON.stringify;`銆傛柟寮忎簩锛氫娇鐢ㄧ涓夋柟搴撶殑`toJSONString`鏂规硶 闄や簡鍘熺敓鐨凧SON瀵硅薄鏂规硶锛涓浜绗笁鏂瑰簱濡俧astjson涔熸彁渚涗簡杞崲...
绛旓細1. 鍦ㄧ嚎宸ュ叿锛氱綉缁滀笂鏈夊緢澶氬厤璐圭殑JSON鏍煎紡鍖栧伐鍏枫備綘鍙渶瑕灏咼SON瀛楃涓澶嶅埗绮樿创鍒拌繖浜涘湪绾垮伐鍏蜂腑锛屽畠浠細鑷姩甯綘鏍煎紡鍖朖SON鏁版嵁锛屽苟鐢熸垚鏄撲簬闃呰鍜岀悊瑙g殑鏍煎紡銆傝繖浜涘伐鍏风壒鍒傚悎蹇熸牸寮忓寲灏忔鐨凧SON鏁版嵁銆2. 缂栫▼璇█鐨勫簱锛氬浜庡紑鍙戣呰岃█锛屽鏋滄鍦ㄤ娇鐢ㄦ煇绉嶇紪绋嬭瑷锛屽彲浠ヤ娇鐢ㄥ搴旂殑搴撴潵鏍煎紡鍖朖SON瀛楃涓层
绛旓細1銆佷娇鐢↗ackson搴擄細import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;public class Main { public static void main(String[] args) throws Exception { // JSON瀛楃涓 String jsonString = "{\"name\":\"John\",\"...
绛旓細灏咼SON瀛楃涓杞崲涓篜ython瀵硅薄 JSON鏁版嵁缁忓父瀛樺偍鍦ㄥ瓧绗︿覆涓傝繖鏄娇鐢ˋPI鏃剁殑甯歌鍦烘櫙銆侸SON鏁版嵁鍦ㄨВ鏋愪箣鍓嶄竴鑸瓨鍌ㄥ湪瀛楃涓插彉閲忎腑銆傚洜姝,涓嶫SON鐩稿叧鐨勬渶甯歌浠诲姟鏄皢JSON瀛楃涓茶В鏋愪负Python瀛楀吀銆侸SON妯″潡鍙互杞绘澗澶勭悊姝や换鍔°傜涓姝ユ槸瀵煎叆Python鐨刯son妯″潡銆傝妯″潡鍖呭惈涓や釜閲嶈鐨勫姛鑳-loads鍜宭oad銆傝娉ㄦ剰,绗竴绉嶆柟娉...