【问题标题】:how to convert curl to ajax?如何将 curl 转换为 ajax?
【发布时间】:2021-02-14 10:47:03
【问题描述】:

并尝试获取 API 密钥。 该网站给了我一些卷曲代码。 好吧,因为我是韩国人,不确定我是否理解了描述。 总之,

    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~~"

这是代码和 我想知道在 ajax 调用代码中将“--data-urlencode”部分放在哪里..

    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);
            }
        });

    })
}

这就是我到达的地方......耻辱...... 当我运行它时,我得到了 CORS 错误。 IBM是这样解释的 'API 网关可以运行 API CORS 操作来处理 API 的跨域资源共享 (CORS) 请求。

默认情况下,API 禁用 CORS,在这种情况下,API 网关会将所有 CORS 请求传递到后端进行处理。当预检请求被传递到后端时,必须为每个可以成为请求目标的路径定义一个 OPTIONS 操作。否则,对该路径的预检请求可能会导致错误。'

所以这意味着他们允许所有访问权限,对吧?

【问题讨论】:

标签: javascript ajax curl


【解决方案1】:

对于 Ajax,3 个选项,我还添加了 ASP 答案,因为它出现在我的 ASP 搜索标签中!

  1. 将此添加到您的 Ajax 调用中的标题中。它仅限于一个站点,您可以使用* 使其允许所有站点(不推荐)

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

   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. Jsonp

    // 如果其他服务器允许 JSONP $.ajax({ 类型:'POST', 跨域:是的, 数据类型:'jsonp', 网址:'', 成功:函数(jsondata){

    } })

  2. 您可以使用 Ajax-cross-origin 一个 jQuery 插件。 使用此插件,您可以使用jQuery.ajax() 跨域。它使用 Google 服务来实现这一点:

AJAX Cross Origin 插件使用 Google Apps 脚本作为代理 JSON 未实现 jSONP 的 getter。当你设置 crossOrigin 选项为 true,插件将原始 url 替换为 Google Apps 脚本地址并将其作为编码的 url 参数发送。谷歌 Apps 脚本使用 Google 服务器资源来获取远程数据,以及 将其作为 JSONP 返回给客户端。

使用下面的插件很容易:

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

您可以阅读更多herehttp://www.ajax-cross-origin.com/

  1. 对于 ASP Web 应用,请在您的 web.config添加以下协议调用选项以允许 CORS 调用。

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

【讨论】:

    猜你喜欢
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 2018-02-15
    • 2016-10-10
    • 2017-05-18
    • 1970-01-01
    相关资源
    最近更新 更多