从现有的javascript写jquery.ajax

时间:2022-10-08 09:50:48

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

我发现这篇文章:http://msdn.microsoft.com/en-us/library/dd251073.aspx

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

我怎么能用jquery.ajax写'get'请求?

3 个解决方案

#1


1  

You could use the .get() method.

您可以使用.get()方法。

#2


0  

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

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.

或者只使用常规的$ .ajax()方法(http://api.jquery.com/jQuery.ajax/),默认为GET请求。

#3


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是否遵守用于指定JSONP回调的标准?callback = function方法,但如果是这样,那么Search()函数的这个简化版本应该这样做:

// 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.

请记住,这两种方法都不会直接触发GET,就像您可能习惯使用XMLHttpRequest对本地服务器的AJAX请求一样。

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.

为了规避对XHR的跨域限制,JSONP的工作原理是在文档中注入一个新的脚本元素,然后导致浏览器加载(通过GET)并执行该远程脚本。该远程脚本的内容是对回调函数的单个函数调用,其中整个JSON有效负载作为其参数。


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

如果这不起作用,包括那些Bing特定的回调选项应该与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).

请记住,在这一点上(以及之前),你根本就没有真正使用jQuery本身。即使$ .getJSON()或$ .ajax()或$ .get()看起来像是在做比MSDN示例更强大的事情,jQuery在这种情况下会做同样的事情(注入一个脚本元素)在srcpointed requestStr)。

#1


1  

You could use the .get() method.

您可以使用.get()方法。

#2


0  

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

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.

或者只使用常规的$ .ajax()方法(http://api.jquery.com/jQuery.ajax/),默认为GET请求。

#3


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是否遵守用于指定JSONP回调的标准?callback = function方法,但如果是这样,那么Search()函数的这个简化版本应该这样做:

// 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.

请记住,这两种方法都不会直接触发GET,就像您可能习惯使用XMLHttpRequest对本地服务器的AJAX请求一样。

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.

为了规避对XHR的跨域限制,JSONP的工作原理是在文档中注入一个新的脚本元素,然后导致浏览器加载(通过GET)并执行该远程脚本。该远程脚本的内容是对回调函数的单个函数调用,其中整个JSON有效负载作为其参数。


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

如果这不起作用,包括那些Bing特定的回调选项应该与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).

请记住,在这一点上(以及之前),你根本就没有真正使用jQuery本身。即使$ .getJSON()或$ .ajax()或$ .get()看起来像是在做比MSDN示例更强大的事情,jQuery在这种情况下会做同样的事情(注入一个脚本元素)在srcpointed requestStr)。