admin管理员组

文章数量:1417413

Dictionary provides absolutely no documentation on how to grab data from them. I'm trying to use jQuery's ajax requests, but those are not working.

They provide a URL through which I'm supposed to be able to use. I'll provide this below.

=<VID>&type=random&site=dictionary

They also provide a key, which I assume that I place in the <VID> spot.

Could someone please tell me what I'm doing wrong with this? I'll provide the code I'm using below:

$("#btnGetData").click(function() {

  $.ajax({
    url: "/",
    type: "GET",
    data: "v001?vid=<VID>&type=random&site=dictionary",
    success: function() { alert("success") },
  });

});

Could someone please tell me what I'm doing wrong?

Dictionary. provides absolutely no documentation on how to grab data from them. I'm trying to use jQuery's ajax requests, but those are not working.

They provide a URL through which I'm supposed to be able to use. I'll provide this below.

http://api-pub.dictionary./v001?vid=<VID>&type=random&site=dictionary

They also provide a key, which I assume that I place in the <VID> spot.

Could someone please tell me what I'm doing wrong with this? I'll provide the code I'm using below:

$("#btnGetData").click(function() {

  $.ajax({
    url: "http://api-pub.dictionary./",
    type: "GET",
    data: "v001?vid=<VID>&type=random&site=dictionary",
    success: function() { alert("success") },
  });

});

Could someone please tell me what I'm doing wrong?

Share Improve this question edited Feb 2, 2013 at 6:31 thordarson 6,2612 gold badges19 silver badges36 bronze badges asked Feb 2, 2013 at 3:53 JaPerk14JaPerk14 1,6643 gold badges26 silver badges33 bronze badges 3
  • Are you replacing <VID> in your code? Is your VID valid? Have you tried submitting to the exact URL in url parameter (instead of writing the query string in the data parameter)? – Marc Commented Feb 2, 2013 at 5:27
  • Did you check the network inspector to find out what HTTP error code you're getting? Install an error callback? Since it's a get you can just type it into the URL box. -1 for a question without error messages, I'll remove it when you add that information, hey, you'll probably answer your own question after you look at those things – Ruan Mendes Commented Feb 2, 2013 at 5:52
  • Are you trying to leverage their "Word of the Day" tool, or is this for something else? – Tieson T. Commented Feb 2, 2013 at 6:47
Add a ment  | 

2 Answers 2

Reset to default 4

You are not passing the data correctly. Also the request url is http://api-pub.dictionary./v001. Try this:

$("#btnGetData").click(function() {
  $.ajax({
    url: "http://api-pub.dictionary./v001",
    type: "GET",
    dataType: "jsonp",  //For external apis
    data: {"vid": <VID>,
          "type":"random"
          "site": "dictionary"},
    success: function() { 
       alert("success") },
  });
});

UPDATE: Maybe you're being blocked by the same origin policy as @thordarson note, in that case add: dataType: "jsonp" to your ajax call.

Possibility 1

You don't have access to their API. According to their API page they're very selective about who they give access to their API.

We are selective about our API partners and approve use as well as develop terms on a case-by-case basis. If you are interested in using our API, please contact us directly [...].

Possibility 2

You're being blocked by the same origin policy. Browsers are generally very strict about requests made by JavaScript across different domains. This can be bypassed using Cross-origin resource sharing. This requires configuring the server you're requesting, so in this case it's not viable.

Your best bet would be to create a server-side script that requests the URL and then using AJAX to request the file.

Example in PHP, let's call this request_dictionary.php:

<?php

    $vid = "Your API key";
    $type = $_GET['type'];
    $site = $_GET['dictionary'];

    $request_url = "http://api-pub.dictionary./v001?vid=$vid&type=$type&site=$site";

    echo file_get_contents($request_url);

?>

Note: You probably need to change this to fit your needs (error handling, 404s, caching etc.). This code is untested.

Then change your jQuery so that it requests your file.

$("#btnGetData").click(function() {

  $.ajax({
    url: "/request_dictionary.php",
    type: "GET",
    data: "type=random&site=dictionary",
    success: function() { alert("success") },
  });

});

Warning

Using an AJAX call without a proxy (as explained in possibility 2) will expose your API key. This is against Dictionary.'s API Terms of Service.

2.1 Dictionary. will assign and deliver to your Customer Application an Application Key to access the API. All Calls must contain such Application Key. The Application Key is uniquely associated with each Customer Application and all versions, upgrades and updates thereof. The Application Key is Confidential Information as defined in these Terms and Conditions. You must maintain and keep the Application Key in a secure, embedded manner not accessible by any third party. The Application Key is fully revocable by Dictionary. at any time. Dictionary. may block attempts to access the API with an invalid or revoked Application Key.

I've updated the code in possibility 2 to conceal the API key by keeping it in the PHP file.


Update/Note: Simply passing dataType: 'jsonp' to enable cross-origin calls is not enough. The responding server needs to respond with an Access-Control-Allow-Origin header containing your domain (or a rule that includes your domain). Any configuration of this kind is out of your hand as the requester.

Update/Note 2: Upon investigating the returned headers from your request URL, I can see no evidence of Access-Control-Allow-Origin. This means that even if you had access to their API, you wouldn't be able to access it using AJAX. Full headers below:

GET /v001?vid=%3CVID%3E&type=random&site=dictionary HTTP/1.1[CRLF]
Host:            api-pub.dictionary.[CRLF]
Connection:      close[CRLF]
User-Agent:      Web-sniffer/1.0.44 (+http://web-sniffer/)[CRLF]
Accept-Encoding: gzip[CRLF]
Accept:          text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8[CRLF]
Accept-Language: en-US,en;q=0.8[CRLF]
Accept-Charset:  ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]
Cache-Control:   no-cache[CRLF]
Referer:         http://web-sniffer/[CRLF]

本文标签: javascriptHow to make an AJAX request to Dictionarycom39s REST API using jQueryStack Overflow