5

I am currently performing 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 gets to the controller and the AddNewProduct function runs successfully. The problem is that i want it to return the image name that is created within the controller. This works as well but with that it also return my complete html page. I alert something on success and when an error occurs but somehow it always ends up in the error with the following alert :

enter image description here

it shows the value I need but why does it return my complete HTML as well?

Niels Peeren
  • 315
  • 1
  • 13
  • 3
    I honestly can't see how that's possible given the code you've shown. – Rory McCrossan Aug 18 '17 at 09:42
  • Use your browser's debugging tools and see what the status code is that is returned from the server. Also debug your server side code and see if there is an exception. – Igor Aug 18 '17 at 09:45
  • @Igor It returns a status code 200 OK – Niels Peeren Aug 18 '17 at 09:48
  • If it returns status 200 I am not sure how you are ending up in the `error` handler... unless that was a mis-type? – Igor Aug 18 '17 at 09:49
  • can you check the content of the 200 ok response ? – Niladri Aug 18 '17 at 09:51
  • @Niladri The content of the response is exactly the same as what it alerts in the error message. i tried to read out what else it outputted and it gives me a parseerror. – Niels Peeren Aug 18 '17 at 09:55
  • Whay you are posted in question it's the actually code ? – Mihai Alexandru-Ionut Aug 18 '17 at 09:56
  • 1
    @Alexandru-IonutMihai yes exactly the same as in my code, a straight copy and paste. – Niels Peeren Aug 18 '17 at 09:57
  • 1
    Is your ajax call is in some Razor view page or is it in some external js file? If it is in some external js file then you can follow the approach given in this answer instead of providing the api URL like below `'url: "@Url.Action("uploadImage", "Item")'` check this approach https://stackoverflow.com/questions/34360537/how-do-i-make-js-know-about-the-application-root – Niladri Aug 18 '17 at 10:01
  • @NielsPeeren check my suggestion above – Niladri Aug 18 '17 at 10:08
  • Have you debugged your server side code? Put a break point in the method and walk though what happens and the value that is returned /passed to the Json method. – Igor Aug 18 '17 at 10:08
  • @Niladri It is not in an external JS file, it is in a Razor view page. – Niels Peeren Aug 18 '17 at 10:10
  • @Igor I debugged it and everything that is being returned is the imagename value. – Niels Peeren Aug 18 '17 at 10:10
  • 1
    Somewhere in the mvc pipe line additional text is being added to the body of the response, you need to figure out where that is. If it is not in the method code you have shown then the other option would be either in an event handler (usually found in global.asax.cs) or in a custom Action Filter (see if there are any registered globally or decorated on top of the class or method). – Igor Aug 18 '17 at 10:13
  • Something just came to mind, I am developing this within the DNN CMS. this is a webform based CMS. Could that have anything to do with this problem? – Niels Peeren Aug 18 '17 at 10:23
  • 1
    Sure, there could be one or more hooks in the pipeline from DNN that would add template code around your response. As to how to circumvent that pipeline I am not sure, you would have to read through the How To on the DNN documentation. You should first look into how you are hooking DNN into your MVC app (or visa-versa). If you want to create a bunch of REST methods the best course of action would be to use Web API instead of MVC as DNN might not assume that you want to return HTML from a Web API call. Try it by adding a Web API controller next to this one and copy/paste the method body – Igor Aug 18 '17 at 10:25
  • 1
    ^----- `continued` - to the Web API method and see if that fixes the problem. You will have to tweak your code a little like returning an IHttpActionResult and return should be Ok with your result passed in. – Igor Aug 18 '17 at 10:28
  • Did that solve your problem? – Igor Aug 18 '17 at 11:06

1 Answers1

3
  • You do not need JsonRequestBehavior.AllowGet. That must be used only for GET requests in order to protect you against a very specific attack involving JSON requests. And in your code you are using POST verb.

  • You should use follow code in order to get the success string received from server.

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

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • Thanks for the tip on the AllowGet, i removed that bit from the code. I also changed my success function in the call but my result remains the same. – Niels Peeren Aug 18 '17 at 09:52
  • 1
    This , combined with the solution found on https://stackoverflow.com/questions/32178142/dotnetnuke-call-ajax-from-a-module fixed my issue. – Niels Peeren Aug 18 '17 at 11:42