views:

688

answers:

2

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

+1  A: 
Pointy
well... the request is working, the data is sent, the ajax response comes up as a success, but there's no data in the response. It is cross site, but as google's spreadsheet api and these 2 resources say it's doable: http://www.west-wind.com/Weblog/posts/107136.aspxhttp://jeffancel.com/archive/2009/04/05/70.aspx http://code.google.com/apis/gdata/docs/json.html
Maslow
You're not doing this with JSONP, however - you're just trying to do a plain XMLHttpRequest. You have to tell jQuery that you want to use JSONP, and the server has to expect that and know to respond with the right sort of JSON/Javascript text.
Pointy
can you explain this in a little more detail? I'm not catching you
Maslow
The way that "JSONP" works is this: you provide jQuery with a URL. jQuery adds a parameter to that URL, a parameter named "callback". That parameter is used by the server to put together a block of Javascript source code to run. To fetch that content, jQuery constructs a new `<script>` block and sets its "src" attribute to this URL. The Javascript returned by the server is then executed in the browser. That is **not** the same as an XMLHttpRequest. Read those very resources that you linked in your comment.
Pointy
ok, so me not getting any data back besides a success message is because I'm using XML http request instead of doing a JSONP. I thought it might have been the server declining to provide content that shows origin and referrer as another domain?
Maslow
You cannot use XMLHttpRequest to make a request to a domain that is different from the domain where the page came from. You **can**, however, make a JSONP request to a server in another domain.
Pointy
well, the request is firing. the other domain is recieving the post data.
Maslow
A: 

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

Maslow