1

I'm trying to return a json result from a jQuery Form instance - but it keeps prompting me to download a file instead of displaying it in the window like it is supposed to...

 $("#ajaxImageForm").ajaxForm({ iframe: true, type: "GET", dataType: "json", beforeSubmit: function() { $("#ajaxImageForm").block({ message: '<img src="/content/images/loader.gif" /> Uploading . . .' }); }, success: function(result) { $("#ajaxImageForm").unblock(); $.growlUI(null, result.message); } }); [AcceptVerbs(HttpVerbs.Post)] public JsonResult Edit(FormCollection collection) { // return Json to the jQuery Form Result return new JsonResult { Data = new { message = string.Format("edited successfully.") } }; } 

    3 Answers 3

    2

    You are requesting a GET from your jQuery code, but you are stating that the action is only for POST. Change one of those and you should be good.

    $("#ajaxImageForm").ajaxForm({ iframe: true, type: "POST", dataType: "json", beforeSubmit: function() { $("#ajaxImageForm").block({ message: '<img src="/content/images/loader.gif" /> Uploading . . .' }); }, success: function(result) { $("#ajaxImageForm").unblock(); $.growlUI(null, result.message); } }); 
    5
    • Nope. I still just get it presented as a downloadable file.
      – Ciel
      CommentedJan 28, 2010 at 20:44
    • it could be something to do with ajaxForm. Have you just tried a standard jQuery ajax?CommentedJan 28, 2010 at 20:46
    • I'm not familiar with how to do it with a standard jQuery ajax.
      – Ciel
      CommentedJan 28, 2010 at 20:47
    • docs.jquery.com/Ajax you seem to be using a ton of jQuery plugins which may be the problemCommentedJan 28, 2010 at 20:53
    • 1
      Thank you very much - the error is because it was flagged to use the iFrame. This in turn was causing it not to be returned to the right window.
      – Ciel
      CommentedJan 28, 2010 at 20:59
    1

    You're setting the ajax request type to "GET", but your action method is set to only accept requests of type POST.

    Try changing it to POST instead.

    1
    • The method itself works - any code I place in there runs. It's the return that doesn't work. It DOES work ,it just presents it as a Downloadable file - instead of jQuery accepting it like it is supposed to.
      – Ciel
      CommentedJan 28, 2010 at 20:45
    0

    I have seen a similar issue, although not sure the reasoning was the same. The way I found to fix this was to return a content result instead with the content type set to text/html.

    i.e.

    return Content("{message: 'edited successfully'}", "text/html"); 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.