asp.netコントローラーで以下のコードを使用して、JavaScriptのAjaxでJsonオブジェクトを返しました
public JsonResult myMethod()
{
// return a Json Object, you could define a new class
return Json(new
{
Success = true, //error
Message = "Success" //return exception
});
}
Jquery-Ajax:
$.ajax({
type: "POST",
url: url_ ,
data: search,
success: function(data) {
//Show Json Properties from Controller ( If Success == false show exception Message from controller )
if (data.Success)
{
alert(data.Message); //display success
}
else
{
alert(data.Message) //display exception
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("error: " + XMLHttpRequest.responseText);
},
dataType: 'json'
});
これはWebApiコントローラーでどのように実行できますか?
参考までに例やURLを教えてください。
よろしくお願いします