【问题标题】:convert a given curl request into ajax request将给定的 curl 请求转换为 ajax 请求
【发布时间】:2018-05-23 09:31:49
【问题描述】:

我有一个curl 请求:

curl -X POST -u "uname":"pwd" --header "Content-Type:application/json" --data "{\"input\": {\"text\": \"Hello there\"}}"https://gateway.watsonplatform.net/assistant/api/v1/workspaces/YourWorkspaceID/message?version=20XX-XX-XX

这个curl 请求的等效ajax 请求是什么?

我试过了,

    $.ajax({
    type: 'post',
    url: "https://gateway.watsonplatform.net/assistant/api/v1/workspaces/XXX...XXX/message?version=20XX-XX-XX",        
    data: "{ \"input\": {\"text\": \"Hello\"}} ",          
    headers : {'Content-Type' : 'application/json' },
    u : { 'uname' : 'pwd' },
    success:function(response)
    {
        console.log("Success!!");
    },

    error : function(xhr, status, error)
    {
        console.log("Status of error message" + status + "Error is" + error);
    }   

    });

请更正 ajax 请求。

【问题讨论】:

    标签: ajax curl watson watson-conversation


    【解决方案1】:

    您必须在 Authorization 标头中发送用户/密码,类似于 https://stackoverflow.com/a/11960692/3331845

    $.ajax({
        type: 'post',
        url: "https://gateway.watsonplatform.net/assistant/api/v1/workspaces/XXX...XXX/message?version=20XX-XX-XX",        
        data: "{ \"input\": {\"text\": \"Hello\"}} ",          
        headers : {'Content-Type' : 'application/json' },
        beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
    },
        success:function(response)
        {
            console.log("Success!!");
        },
    
        error : function(xhr, status, error)
        {
            console.log("Status of error message" + status + "Error is" + error);
        }   
    
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-06
      • 2016-10-10
      • 2018-02-16
      • 1970-01-01
      • 2018-04-16
      • 2021-02-06
      • 2017-01-13
      相关资源
      最近更新 更多