API call inserting question mark "?" to the end of every request I make - JavaScript

Hello all!

Every time I make a call to the Oxford Dictionaries API my query has a "?" inserted at the end. I know the original word and request I send is correct as I log it, and if I switch the /stats/frequency/word/en/?corpus=nmc&lemma=hello out for /inflections/en/hello it gives me the correct word definition. I was wondering if anyone has any ideas on what I am doing wrong? I posted an example of my code and what it would return below:


ajax({
url: my url,
type: 'GET',
dataType: 'json',
data:{
reqUrl: https://od-api.oxforddictionaries.com:443/api/v1/stats/frequency/word/en/?corpus=nmc&lemma=hello,
xmlToJSON: "false",
proxyHeaders:{
"Accept": "application/json",
"app_id": app_id,
"app_key": app_key
},
}
}).then((data) =>{
console.log(data)
});

Would log the following:


metadata:Object
language:"en"
options:Object
corpus:"nmc"
lemma:"hello?"
proto:Object
provider:"Oxford University Press"
proto:Object
result:Object
frequency:19
lemma:"hello?"
matchCount:2
normalizedFrequency:0.002123964369995
proto:Object
proto:Object

Also... while I am here, would I be able to get the lexical statistics for a few random words in the same call? i.e. hello, goodbye, turkey, parenthesis, etc...

Thank you!

Answers

  • Hello @kurtwilliam,

    I had a look at your code and if I understand the ajax call correctly, you are actually making the call to the API through some proxy. I think that is mangling the URL. When I perform a call directly from jquey, the results are as expected. Here is a minimal working example:

        <!DOCTYPE html>
        <html>
        <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
          <script>
          $(document).ready(function(){
            $.ajaxSetup({
              headers: {
                "Accept": "application/json",
                "app_id": '35463034',
                "app_key": '0d9c454552510d207f2e82d7a8284d41'
              }
            });
    
            $.ajax({
            url:'https://od-api.oxforddictionaries.com:443/api/v1/stats/frequency/word/en/',
            type: 'GET',
            dataType: 'json',
            data:{
              "lemma": "test"
            }
            }).then((data) =>{
              console.log(data);
              $('body').text(JSON.stringify(data));
            });
    
          });
          </script>
        </head>
        <body>
    
        </body>
        </html>
    

    Perhaps a solution could be to use POST - form data or json (works only for lexistats though):

            $.ajax({
            url:'https://od-api.oxforddictionaries.com:443/api/v1/stats/frequency/word/en/',
            type: 'POST',
            dataType: 'json',
            data: {
              "lemma": "test"
            }
            })
    

    As to your second question, you can retrieve multiple results in one call (up to 10 items) using the /words/ endpoint but you will have to "collate" the results yourself as it returns a list. That is, you will have to iterate through the results and add up all the frequencies with the same lemma. Here is an example of the request (note the different URL):

            $.ajax({
            url:'https://od-api.oxforddictionaries.com:443/api/v1/stats/frequency/words/en/',
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify({
              "lemmas": ["test", "unit"]
            })
            })
    

    Hope that helps.

    Best,
    Roman

  • Thanks Roman! That definitely does help. I believe I used the proxy because I was getting a 403 error last year - haven't gotten it working yet but will keep trying. I am still getting 403 errors without the proxy (OPTIONS https://od-api.oxforddictionaries.com/api/v1/stats/frequency/word/en/?lemma=test 403 (Forbidden))!

    Will keep you posted,

  • That is strange. It should work just fine without the proxy. Are you passing the headers on every call or do you call $.ajaxSetup({ headers: { "Accept": "application/json", "app_id": '...', "app_key": '...'} }); ?

  • kurtwilliamkurtwilliam Member
    edited January 2018

    I am passing the headers within the call - I wasn't able to get the $.ajaxSetup to work. I should continue trying to get the call working with ajaxSetup then?

    @rkutlak129

  • rkutlak129rkutlak129 Member
    edited January 2018

    ajaxSetup should set the headers for all following ajax calls but it is not necessary. You can pass the headers on each call as you are doing. As long as you get what you need it doesn't really matter.

    @kurtwilliam

Sign In or Register to comment.