How do I make an AJAX request to the Dictionary.com REST API using jQuery?

Dictionary.com contains absolutely no documentation on how to grab data from them. I am trying to use jQuery ajax requests but they are not working.

They provide a url through which I have to use. I'll give it below.

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

      

They also provide a key, which I assume I am putting it in <VID>

.

Can someone please tell me what am I doing wrong with this? I will provide the code that I am using below:

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

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

});

      

Can someone please tell me what I am doing wrong?

+3


source to share


2 answers


You are not transferring data correctly. See also the request url http://api-pub.dictionary.com/v001

. Try the following:

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

      



UPDATE: You might be blocked by the same origin policy as @thordarson's note, in this case Add: dataType: "jsonp"

to your ajax call.

+4


source


Option 1

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

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

Option 2

You are blocked by the same origin policy . Browsers tend to be very strict about requests made by JavaScript across different domains. You can work around this by using Resource Sharing . This requires setting up the server you are requesting, so it is not viable in this case.

Your best bet would be to create a server side script that asks for the url and then use AJAX to request the file.

Example in PHP, let this request_dictionary.php

:

<?php

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

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

    echo file_get_contents($request_url);

?>

      

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

Then change your jQuery to request your file.



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

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

});

      

Attention!

Using an AJAX call without a proxy (as explained in Capability 2) will expose your API key. This is contrary to the Dictionary.com API Terms of Service .

2.1 Dictionary.com will assign and deliver to your client application an application key to access the API. All calls must contain this application key. The application key is unique associated with each client application and all versions, updates and updates. Application Key is confidential information as defined in these Terms. You must maintain and maintain the Application Key in a secure, embedded form that is not accessible to any third party. The application key is completely canceled by Dictionary.com at any time. Dictionary.com can block attempts to access the API using an invalid or revoked application key.

I updated the code in feature 2 to hide the API key by storing it in a PHP file.


Update / Note: A simple walkthrough is dataType: 'jsonp'

not enough to enable cross-error calls. The response server needs to respond with a header Access-Control-Allow-Origin

containing your domain (or a rule that includes your domain). Any configuration of this type is out of your hand as a requester.

Update / Note 2: . Examining the returned headers from your request url, I see no evidence whatsoever Access-Control-Allow-Origin

. This means that even if you have access to their API, you will not be able to access it using AJAX. The full list of titles is below:

GET /v001?vid=%3CVID%3E&type=random&site=dictionary HTTP/1.1[CRLF]
Host:            api-pub.dictionary.com[CRLF]
Connection:      close[CRLF]
User-Agent:      Web-sniffer/1.0.44 (+http://web-sniffer.net/)[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.net/[CRLF]

      

+3


source







All Articles