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);
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

Nope. I still just get it presented as a downloadable file.
it could be something to do with ajaxForm. Have you just tried a standard jQuery ajax?
I'm not familiar with how to do it with a standard jQuery ajax.
docs.jquery.com/Ajax you seem to be using a ton of jQuery plugins which may be the problem
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.
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 Comment

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.
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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.