javascriptjqueryajaxget

Setting Accept-Language header in AJAX request


I am using an API and in the GET request it wants this:

Accept: application/json
Accept-Language: sv
From: my@domain.com

My code looks like this:

var full_url = "http://api.arbetsformedlingen.se/af/v0/platsannonser/matchning?nyckelord=it";
$.ajax({
  type: 'GET',
  url: full_url,
  dataType: 'json',
  headers: {
    Accept: 'application/json', 
    from: 'my@domain.com', 
    accept-language: 'sv'
  }
});

When I add accept-language: sv I get this:

Unexpected token -

What am I doing wrong here? The API is saying that it needs to have those 3 parameters for it to work.


Solution

  • The issue is because you need to wrap the header names in quotes otherwise the - is interpreted by JS as a subtraction operator - hence why it is unexpected within the object. Try this:

    headers: {
        'Accept': 'application/json', 
        'From': 'my@domain.com', 
        'Accept-Language': 'sv'
    }