我习惯于在我的应用程序中使用jQuery而且我正在做这样的ajax请求:
function save_marker(Marker, mRuas, replaceWin)
{
//Save new marker using jQuery Ajax
var mLatLang = Marker.getPosition().toUrlValue(); //get marker position
var myData = {ruas : mRuas, latlang : mLatLang }; //post variables
console.log(replaceWin);
$.ajax({
type: "POST",
url: "maps/map_process.php",
data: myData,
success:function(data){
replaceWin.html(data); //replace info window with new html
Marker.setDraggable(false); //set marker to fixed
Marker.setIcon('img/b.png'); //replace icon
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError); //throw any errors
}
});
}
如果我在“网络”标签下的Chrome开发者工具中查看我的ajax请求,我会看到ruas和latlang像Form Data一样发送。 表单数据: ruas:1234 latlang:-8.459511,115.066144
最近我正在测试上面的ajax请求,我这样做:
var dataPath = {
tahun : year,
kawasan : areal,
path : pathSend,
idGenangan : jmlRecord
};
$.ajax({
url :'maps/modul/genangan-sql-drawing.php',
type :'POST',
dataType:'json',
data : JSON.stringify(dataPath),
contentType:"application/json",
success: function(data, xhr){
alert("Sukses Menggambar Genangan");
},
error:function(xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
我注意到数据发送的不同之处。在这种情况下,数据就像Request Payload一样发送。
**Request Payload**
{"tahun":"2012","kawasan":"Kawasan Genangan 2012","path":[{"lat":-8.240032737708358,"lng":115.11680603027344},{"lat":-8.331082833500302,"lng":114.98222351074219},{"lat":-8.363692651835823,"lng":115.26374816894531}],"idGenangan":1}
**Request Header**
POST /bootstrap/maps/modul/genangan-sql-drawing.php HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 230
Accept: application/json, text/javascript, */*; q=0.01
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Content-Type: application/json
Referer: http://localhost/bootstrap/area-genangan.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,id;q=0.6
问题是: 1.请求有效负载和表单数据之间有什么不同? 2.为什么我收到请求有效载荷? 3.如果我想用表单数据更改请求有效负载。我该怎么办?
我希望有人理解我的意思并且可以帮助我。