开发者

Loading XML from an API

开发者 https://www.devze.com 2023-02-07 06:49 出处:网络
Using jQuery, I tried to load a definition file from Dictionary.com\'s API: var tmpWord = \"hello\"; jQuery.ajax({

Using jQuery, I tried to load a definition file from Dictionary.com's API:

var tmpWord = "hello";    
jQuery.ajax({
            type: "GET",
            url: "http://api-pub.dictionary.com/v001?vid=<MYKEY>&q="+tmpWord+"&site=dictionary",
            dataType: ($.browser.msie) ? "text/xml" : "xml",
            success: function(xml) {
                console.log($(this).text());
            }
        });

(NOTE: I've replaced my API key with the thing on purpose so you lot don't steal it :P)

Anyway, for some reason, this will return a result in IE but not Firefox :'( and this is the same even if I force dataType: 'xml' Any开发者_JS百科 ideas gurus?

Cheers.

Neuro


Isn't this a cross domain scripting issue? That's not allowed. IE gives you the option to override the settings based on your security level however firefox won't allow cross domain scripting

Wikipedia Article:Cross-site scripting

Maybe i have the context wrong but that's my 2 cents.

Also why would you do this on the client if anyone can just read your ApiKey in the source?


Are you trying to just access the XML as a text string?

If so, this should do the trick on the xml object

string = new XMLSerializer().serializeToString( xml )


Ok, so I managed to sort this by using the following statement:

window.dict_api_callbacks = function(theObj) {
        window.result = theObj.primaries[0].entries[1].terms[0].text;
    }

    $.getScript("http://www.google.com/dictionary/json?callback=dict_api_callbacks&q="+tmpWord+"&sl=en&tl=en&restrict=pr%2Cde&client=te", function() { 
    //code
});

...and then returning window.result as the item's text. I gave up on the Dictionary.com API - it seems rough and awkward when compared to generic Google "unofficial" API.

Thank you anyway - I have distributed points.

N

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号