Accessing API via web app

I am trying to retrieve all the assignments for a given questionnaire using jquery ajax.

Here is a sample code.

$.ajax({
dataType: ‘jsonp’,
url: ‘https://domain.gov.gd/api/v1/assignments?QuestionnaireId=a189a582c01a41129058b44e799815af$6’,
beforeSend: function(xhr) {
xhr.setRequestHeader(“Authorization”, “Basic KeyGeneratedBySwaggerUI”);
}, success: function(data){
alert(data);
}
})

This result in a 401 error (Unauthorized) but the same key works fine with curl via the terminal.

I set the data type to jsonp because I was getting an access-control-allow-origin error. If I remove the data type option I get a 405 error (Method Not Allowed).

Please help.

Dear Andre,

I am afraid that this approach will not work because of the CORS policy on the server, you will find the mentioning of this in the Console when debugging your web page. The same works in Swagger because it has the same origin, and in curl because it doesn’t verify the CORS policy.

Perhaps you can revise the architecture of your solution to have the client submit the request to your custom solution server, and in turn pass it to the Survey Solutions server (e.g. as a query from PHP, or similar).

Best, Sergiy

Thank you for your fast reply.

Would this work if I host my app on the same server as the survey solutions?

Can you please elaborate on your suggested approach.

ihmo yes, if it is a different web-server, albeit on the same server.

Okay Thanks.

Currently I don’t have access to the server hosting survey solutions, I will need to request access. Meanwhile, can you explain what you meant by “custom solution server” and how will I be able to make an api call to survey solutions without violating the CORS policy?

Thanks in advance,
Andre.

you need to make sure that the query to the Survey Solutions server is submitted not directly from a browser, albeit may originate in a browser, hence the need for something in the middle, like the mentioned custom server:

Thank you very much, I understand.