0

I found this article: http://msdn.microsoft.com/en-us/library/dd251073.aspx

How could I write 'get' request using jquery.ajax?

2
  • What is it you really want? Why do you need get requests? Moreover, don't forget to use encodeURIComponent to properly encode query parameters. They don't do that in the linked example. Commented May 12, 2011 at 21:17
  • -1 for linking to an article that doesn't clearly relate to the question, as well as for a question that isn't clear about what it is even asking. here's a shot in the dark to help you though: api.jquery.com/jQuery.ajax Commented May 12, 2011 at 21:17

3 Answers 3

1

You could use the .get() method.

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

Comments

0

http://api.jquery.com/jQuery.get/

Or just use the regular $.ajax() method (http://api.jquery.com/jQuery.ajax/), which defaults to a GET request.

Comments

0

It depends on whether Bing's API respects the standard ?callback=function method for specifying JSONP callbacks, but if so, then this simplified version of the Search() function should do it:

// Bing API 2.0 code sample demonstrating the use of the
// Spell SourceType over the JSON Protocol.
function Search()
{
        var requestStr = "http://api.bing.net/json.aspx?"

        // Common request fields (required)
        + "AppId=" + AppId
        + "&Query=Mispeling words is a common ocurrence."
        + "&Sources=Spell"

        // Common request fields (optional)
        + "&Version=2.0"
        + "&Market=en-us"
        + "&Options=EnableHighlighting"

        $.getJSON(requestStr, SearchCompleted);
}

Keep in mind that neither approach is directly triggering a GET, like you might be used to in AJAX requests to a local server using XMLHttpRequest.

To circumvent the cross-domain restriction on XHR, JSONP works by injecting a new script element into your document which then causes the browser to load (via GET) and execute that remote script. That remote script's contents are a single function call to your callback function, with the entire JSON payload as its parameter.


If that doesn't work, including those Bing-specific callback options should work fine in conjunction with jQuery:

// Bing API 2.0 code sample demonstrating the use of the
// Spell SourceType over the JSON Protocol.
function Search()
{
        var requestStr = "http://api.bing.net/json.aspx?"

        // Common request fields (required)
        + "AppId=" + AppId
        + "&Query=Mispeling words is a common ocurrence."
        + "&Sources=Spell"

        // Common request fields (optional)
        + "&Version=2.0"
        + "&Market=en-us"
        + "&Options=EnableHighlighting"

        // JSON-specific request fields (optional)
        + "&JsonType=callback"
        + "&JsonCallback=SearchCompleted";

        $.getJSON(requestStr);
}

Keep in mind that, at this point (and somewhat before), you aren't really using jQuery itself for much at all. Even though $.getJSON() or $.ajax() or $.get() seem like they're doing something more powerful than the MSDN example, jQuery is going to do exactly the same thing in this case (inject a script element with its srcpointed at requestStr).

2 Comments

Callback function doesn't execute. I looked at this request in Fiddler and I saw staus:200 and response text.But I have no ideas how I can execute callback function. Could you help me?
Updated with another approach.

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.