0

I am a beginner programmer trying to form a URL to GET macy product information based on my keyword, here:http://developer.macys.com/docs/read/catalog_and_store_services/catalog/search#requesturl
They have 3 mandatory parameters, 2 of which is in the HTTP header, Accept and x-macys-webservice-client-id with another being in the query parameter, searchphrase. enter image description here According to the document, I have formed a jQuery function to GET products information:

function ajaxsearch(){
    var terms = "reddress";
    var macyurl = "https://api.macys.com/v4/catalog/search?searchphrase="+ terms;
    alert(macyurl);
    var apikey = "6zjre5gv8822t323e6wxj85a";
     $.ajax({
        url: macyurl,
        headers:{
        Accept: application/json,
        x-macys-webservice-client-id: apikey
        }
        success: function(data){
        alert("successful call!") 

        }

    });

}

Question: Are the syntax of my function correct? I have checked this with the console and they are having problems with one of the headers, x-macys-webservice-client-id. Is this the correctly way to set up HTTP header parameters in my case?

Detective merry
  • 384
  • 1
  • 21

1 Answers1

1

Keys of an object should follow the same rules of naming a variable. If not, they should be quoted like this:

headers:{
    Accept: "application/json", // you can quote the key here too, but the value has to be quoted.
    "x-macys-webservice-client-id": apikey // the key must be quoted since - aren't allowed in key names. if apikey is not a variable, then it should be quoted too.
}, // Plus, you forgot to put a comma here to separate the entries

NOTE: If you don't know what do keys and values mean here is what I'm talking about:

{
    key: value,
    anotherKey: anotherValue,
    // ...
}
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • Thanks for your reply! I am going to mark this as answer as you have helped me solve my syntax error. Although the request still didn't work probably because macy require cURL from php. – Detective merry Jan 29 '17 at 17:39
  • 1
    @Detectivemerry I don't know about Macy. But the errors you had regarding the `headers` object are solved. – ibrahim mahrir Jan 29 '17 at 17:41
  • 1
    @Detectivemerry I've just tried your code and it shows in the console (in may case) that it's a CORS (cross-origin ressource sharing) error because I'm running it on my localhost. If your error is the same, look how to solve it using that information (CORS). See if [this](http://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin-resource-sharing-cors-post-request-working) can help. – ibrahim mahrir Jan 29 '17 at 17:59
  • Thank you, again, I will research about this! – Detective merry Jan 29 '17 at 18:15