0

and trying to get an API key. and the website gave me some curl code. well since im a korean, not sure whether i understood the description right or not. anyways,

    curl --insecure -X POST 
   --header "Content-Type: application/x-www-form-urlencoded" 
   --header "Accept: application/json" 
   --data-urlencode "grant_type=urn:grant-type:apikey"
   --data-urlencode "apikey=$API_KEY" "https://iam~~"

this is the code and I wanna know where to put '--data-urlencode' part on an ajax call code..

    function to_ajax(){
    $.getJSON("http://localhost~~/",
    function(data){
        $.ajax({
            dataType: 'application/json', 
            data : {"grant_type":"urn:grant-type:apikey"
                    ,"apikey=$API_KEY":"https://iam~~"},
            headers: {"Content-Type" : "application/x-www-form-urlencoded" , 
            "Accept": "application/json" } ,
            success: function(msg) {
              console.log(msg);
            }
        });

    })
}

this is where i've been reached...shame.. and when I run this, I get CORS error. and the IBM explained like this 'The API Gateway can run the API CORS action to handle cross-origin resource sharing (CORS) requests for an API.

CORS is disabled for an API by default, in which case the API Gateway passes all CORS requests to the back end for handling. When preflight requests are passed to the back end, an OPTIONS operation must be defined for each path that can be the target of the request. Otherwise, the preflight request to that path can result in an error.'

so this means they allowed all access, right?

bonlim
  • 163
  • 9
  • Does this answer your question? [Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i) – derpirscher Nov 01 '20 at 16:58

1 Answers1

0

For Ajax, 3 options, I also added ASP answer since it showed up in my ASP search tags!

  1. add this to your header in your Ajax call. Its limited to one site, you can use a * to make it allow all (not recommended)

headers: { 'Access-Control-Allow-Origin': 'http://The web site you are specially allowing to access' }, // or use *

   function to_ajax(){
    $.getJSON("http://localhost~~/",
    function(data){
        $.ajax({
            dataType: 'application/json', 
            data : {"grant_type":"urn:grant-type:apikey"
                    ,"apikey=$API_KEY":"https://iam~~"},

            headers: { 'Access-Control-Allow-Origin': 'http://TCUrl stack overflow' }, //only a specific site
            headers: {'Access-Control-Allow-Origin': '*'}, //or allow everybody  

            headers: {"Content-Type" : "application/x-www-form-urlencoded" , 
            "Accept": "application/json" } ,
            success: function(msg) {
              console.log(msg);
            }
        });

    })
  1. or with Jsonp

    // this if the other server allows JSONP $.ajax({ type: 'POST', crossDomain: true, dataType: 'jsonp', url: '', success: function(jsondata){

    } })

  2. You can use Ajax-cross-origin a jQuery plugin. With this plugin you use jQuery.ajax() cross domain. It uses Google services to achieve this:

The AJAX Cross Origin plugin use Google Apps Script as a proxy jSON getter where jSONP is not implemented. When you set the crossOrigin option to true, the plugin replace the original url with the Google Apps Script address and send it as encoded url parameter. The Google Apps Script use Google Servers resources to get the remote data, and return it back to the client as JSONP.

Easy to use with the plugin below:

    $.ajax({
        crossOrigin: true,
        url: url,
        success: function(data) {
            console.log(data);
        }
    });

You can read more here: http://www.ajax-cross-origin.com/

  1. For ASP Web apps, inside your web.config please add the following protocall option to allow CORS calls.

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
</httpProtocol>

Transformer
  • 6,963
  • 2
  • 26
  • 52