views:

302

answers:

4

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.") } };
    }
+1  A: 

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.

womp
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.
Stacey
+2  A: 

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);
    }
});
Nick Berardi
Nope. I still just get it presented as a downloadable file.
Stacey
it could be something to do with ajaxForm. Have you just tried a standard jQuery ajax?
Nick Berardi
I'm not familiar with how to do it with a standard jQuery ajax.
Stacey
http://docs.jquery.com/Ajax you seem to be using a ton of jQuery plugins which may be the problem
Nick Berardi
You were correct. it is an error with jQueryForm.
Stacey
Yeah, but the only way to learn is to try, right?
Stacey
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.
Stacey
A: 

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");
channer
A: 

Have You all tested it with mozilla? Because mozilla behave strangely when upload the image, the json result is show in the page rather than the original page when I upload the image. So my mozilla page just show the json only. It behave differently than IE and Chrome.

Thank you

Yugi