Ajax call returns values ​​and my html page

I am currently making an ajax call to my controller with the following code:

$.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); } }); 

This is the controller:

 [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); } 

It goes to the controller and the AddNewProduct function succeeds. The problem is that I want it to return the image name created in the controller. This works, but with that, it also returns my full html page. I warn something about success and when an error occurs, but for some reason it always ends with an error with the following warning:

enter image description here

shows the value I need, but why does it also return my full HTML code?

+5
source share
1 answer
  • You do not need JsonRequestBehavior.AllowGet . This should only be used for GET requests to protect you from a very specific attack involving JSON requests. And in your code you use the POST verb.

  • You must use the following code to get the success string received from the server.

    success: function (response) { alert('Success ' + response.success); }

+3
source

Source: https://habr.com/ru/post/1270989/


All Articles