[英]ajax call returns values and my html page
我目前正在使用以下代码对我的控制器执行ajax调用:
$.ajax({
type: "POST",
url: "@Url.Action("uploadImage", "Item")",
data: '{ "imageData" : "' + image + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (success) {
alert('Success ' + success.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
这是控制器:
[HttpPost]
public ActionResult uploadImage(string imageData)
{
string imageName = Guid.NewGuid().ToString();
try
{
ProductManager pm = new ProductManager();
pm.AddNewProduct(imageName);
}catch(Exception e)
{
writeToLog(e);
}
return Json(new { success = imageName }, JsonRequestBehavior.AllowGet);
}
它到达控制器并且AddNewProduct函数成功运行。 问题是我希望它返回在控制器中创建的图像名称。 这也可以,但是它也会返回我完整的html页面。 我在成功时发出警告,并在发生错误时发生错误,但不知怎的,它总是在错误中出现以下警告:
它显示了我需要的价值,但为什么它也会返回我的完整HTML?
您不需要JsonRequestBehavior.AllowGet
。 这必须仅用于GET
请求,以保护您免受涉及JSON请求的特定攻击。 在您的代码中,您使用的是POST
动词。
您应该使用以下代码以获取从服务器收到的success
字符串。
success: function (response) { alert('Success ' + response.success); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.