Maslow
Maslow

Reputation: 18746

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

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

Upvotes: 1

Views: 1730

Answers (2)

Maslow
Maslow

Reputation: 18746

You can send data to another domain using regular get/post, but no data can come back. to pull data back you need to use JSONP

Upvotes: 1

Pointy
Pointy

Reputation: 413682

When it's run "remotely" — on a real server — what's the domain name? What is the URL that's the target of the Ajax request? Those domains have to be the same, you know. You cannot deploy to http://your.application.domain/foo and then issue Ajax requests to http://some.other.domain/bar because of security restrictions.

edit sorry I take that back; not enough coffee this morning :-)

Upvotes: 1

Related Questions