1

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

2 Answers 2

1

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 :-)

Sign up to request clarification or add additional context in comments.

7 Comments

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: west-wind.com/Weblog/posts/107136.aspx jeffancel.com/archive/2009/04/05/70.aspx code.google.com/apis/gdata/docs/json.html
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.
can you explain this in a little more detail? I'm not catching you
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.
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?
|
1

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

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.