避免在Rest API中使用CORS

I'm having a problem to make a call to a rest API.

In the document (FAQ) of the web application there is an example that use AJAX request to make the call. Here an example:

var url = 'https://example.com/yyy';

$.ajax({
    type: 'POST',
    url: url,
    cache: false,
    data: {
        opt: JSON.stringify(example)
    },
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (result)
    {
        console.log(result);
    } });

I created a local page with this code to made the post to the API that is located on a remote server but I receive an error about CORS.

Is there any solution to circumvent this problem? I tried to use firefox plugin to allow CORS but it didn't solve the problem. The session is authenticated via form before use the endpoint.

I see several issues:

  1. Try to run the code from a domain and not from local disk (alternatively you can consider using https://crossorigin.me/ )
  2. How does the authentication work? if with cookies you need to add withCredentials to the ajax request.
  3. Make sure the API returns Access-Control-Allow-Origin: foo header where foo is the domain your code runs in. If you also used withCredentials, you should add Access-Control-Allow-Credentials: true