不使用contentType: “application/json”则data可以是对象
$.ajax({ url: actionurl, type: "POST", datType: "JSON", data: { id: nodeId }, async: false, success: function () {} })
使用contentType: “application/json”则data只能是json字符串
$.ajax({ url: actionurl, type: "POST", datType: "JSON", contentType: "application/json" data: "{'id': " + nodeId +"}", async: false, success: function () {} });
上面的例子是直接拼接的,还是可使用json.stringify()函数来将对象解析为字符串格式。
function getAjaxJson(tradeCode, data) { var json = JSON.stringify({ "clientTime": localTime,
"data": data, "deviceId": getDeviceid(),
"token": getToken(), "tradeCode": tradeCode,
"version": getVersion() }) return json; }