将给定的curl请求转换为ajax请求

时间:2018-05-23 09:31:50

标签: ajax curl watson watson-conversation

我有一个 fifos(0).suggestName("FirstFifo") fifos(1).suggestName("SecondFifo") 请求:

curlhttps://gateway.watsonplatform.net/assistant/api/v1/workspaces/YourWorkspaceID/message?version=20XX-XX-XX

curl -X POST -u "uname":"pwd" --header "Content-Type:application/json" --data "{\"input\": {\"text\": \"Hello there\"}}"请求的等效ajax请求是什么?

我试过了,

curl

请更正ajax请求。

1 个答案:

答案 0 :(得分:0)

您必须在授权标头中发送用户/密码,类似于此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);
    }   

    });