Client端
$.ajax({
url: '/tbRMAs/_Details',
type: 'get',
data: { id: '202'}, //發送的Parameter
dataType: 'json', //取得的資料型態
success: function (d) {
console.log(d);
},
error: function (error) {
console.log(error);
}
});
Server端
[HttpGet]
public ActionResult _Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tbRMA tbRMA = db.tbRMA.Find(id);
if (tbRMA == null)
{
return HttpNotFound();
}
return Json(tbRMA);
}
回傳出現錯誤訊息
直接將導向網頁出的的異常訊息
此要求已被封鎖,因為用於 GET 要求時無法向第三方網站揭露敏感資訊。若要允許 GET 要求,請將 JsonRequestBehavior 設定為 AllowGet。
將Server的return Json(tbRMA);
修改成return Json(tbRMA, JsonRequestBehavior.AllowGet);
雖可正常回傳資料, 但不建議這樣操作, 因msdn描述當用在GET請求中, 會將敏感訊息透漏給第三方, 因此建議還是以Post為主.
改用Ajax Post
Client端
$.ajax({
url: '/tbRMAs/_Details',
type: 'post',
data: { id: '202'}, //發送的Parameter
dataType: 'json', //取得的資料型態
success: function (d) {
console.log(d);
},
error: function (error) {
console.log(error);
}
});
Server端
[HttpPost]
public ActionResult _Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tbRMA tbRMA = db.tbRMA.Find(id);
if (tbRMA == null)
{
return HttpNotFound();
}
return Json(tbRMA);
}