Asp.net MVC jQuery Ajax calls to JsonResult return no data

Posted by Maslow on Stack Overflow See other posts from Stack Overflow or by Maslow
Published on 2010-03-06T13:28:06Z Indexed on 2010/03/12 14:47 UTC
Read the original article Hit count: 617

I have this script loaded on a page:

(function() {
            window.alert('bookmarklet started');
            function AjaxSuccess(data, textStatus, xmlHttpRequest) {
                if (typeof (data) == 'undefined') {
                    return alert('Data is undefined');
                }
                alert('ajax success' + (data || ': no data'));
            }
            function AjaxError(xmlHttpRequest, textStatus, errorThrown) {
                alert('ajax failure:' + textStatus);
            }
            /*imaginarydevelopment.com/Sfc*/
            var destination = { url: 'http://localhost:3041/Bookmarklet/SaveHtml', type: 'POST', success: AjaxSuccess, error: AjaxError,
                dataType: 'text',contentType: 'application/x-www-form-urlencoded'
            };
            if (typeof (jQuery) == 'undefined') {
                return alert('jQuery not defined');
            }

            if (typeof ($jq) == 'undefined') {
                if (typeof ($) != 'undefined') {
                    $jq = $;
                } else {
                    return alert('$jq->jquerify not defined');
                }
            }
            if ($jq('body').length <= 0) {
                return alert('Could not query body length');
            }
            if ($jq('head title:contains(BookmarkletTest)').length > 0) {
                alert('doing test');
                destination.data = { data: 'BookmarkletTestAjax' };
                $jq.ajax(destination);
                return;
            }

        })();

when it is run locally in VS2008's cassini the ajax success shows the returned string from Asp.net MVC, when it is run remotely the ajax success data is null. Here's the controller method that is firing both locally and when run remotely:

    [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
    public string SaveHtml(string data)
    {
        var path = getPath(Server.MapPath);
        System.IO.File.WriteAllText(path,data);
        Console.WriteLine("SaveHtml called");
        Debug.WriteLine("SaveHtml called");

        //return Json(new { result = "SaveHtml Success" });
        return "SaveHtml Success";
    }

Once i have it working I was going to remove the GET, but currently accessing the SaveHtml method directly from the webbrowser produces the expected results when testing.

So there's something wrong in my javascript I believe, because when I step through there with chrome's developer tools, I see the data is null, and the xmlHttpRequest doesn't appear to have the expected result in it anywhere either.

I'm loading jquery via http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jquery-ajax