javascript - cURL 到 xmlhttprequest

标签 javascript ajax curl oauth-2.0 xmlhttprequest

有人知道如何将以下curl命令转换为ajax/xmlhttpsrequest吗?

curl -d "username=username@company.com&client_secret=123456789&password=pwdgoeshere&grant_type=password&client_id=ReallyLongClientIDGoesHere" -H "Accept: application/json" https://login.salesforce.com/services/oauth2/token

我想实现类似以下两个示例的目标,但我的代码不起作用:

1)

var req = new  XMLHttpRequest();
  req.open( "POST", "https://login.salesforce.com/services/oauth2/token", true); 
  req.setRequestHeader('Accept', 'application/json');
  req.send();

2)

 $.ajax({
    url: myUrl,
    headers: myHeaders,
    type: 'POST',
    processData: false,
    contentType: false,

}).complete(function ( data ) {
    console.log(data.responseText);
});

最佳答案

我认为你需要这样的东西:

$.ajax({
    url: "myUrl",
    data: { 
        "username": username, 
        "VarB": VarB, 
        "VarC": VarC
    },
    cache: false,
    type: "POST",
    success: function(response) {

    },
    error: function(xhr) {

    }
});

关于javascript - cURL 到 xmlhttprequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759100/

相关文章:

php - 将 Session 变量传递给称为 PHP 脚本的 ajax

php - 使用 ajax 和 php 页面检索列表

javascript - 每次与其他元素发生碰撞时调用函数

javascript - 使用现有 Canvas 而不丢失 pIxi.js 中的内容

jquery - 在 AJAX 响应中使用 js.haml 渲染部分内容

c++ - 如何在 C++ 应用程序中使用 curl Nuget 包

php - GoDaddy PayPal API 警报握手失败

curl - 从Kubernetes Pod到ExternalName服务的cURL挂起

javascript - 在 Javascript 中表示复杂/嵌套数据

javascript - Angular 嵌套/动态 Handlebars