【发布时间】:2015-12-22 11:15:43
【问题描述】:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<h3>REST API With Javascript</h3>
<button id="connect">Connect</button>
<script type="text/javascript">
$(document).ready(function(){
function Contact(access_token, instance_url, id, token_type, issued_at, signature) {
this.access_token = access_token;
this.instance_url = instance_url;
this.id = id;
this.token_type = token_type;
this.issued_at = issued_at;
this.signature = signature;
this.toJsonString = function () {
return JSON.stringify(this);
}
}
$('#connect').click(function(){
jQuery.ajax({
type: "POST",
url: "https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=3MVG9ZL0ppGP5UrBcvLyXYRmRUuYKSgFgQayclpZBWmbi7PQ2vWj1V8Xev.GfE0C9xyWMrTzONlj2GZ0ow6VK&client_secret=2150505966475551576&username=manoj6893@gmail.com&password=9989832835j4oKKODMzqViqjJ2ORFEogUokh",
data: Contact,
dataType: "application/json; charset=utf-8",
success: success_callback,
error: error_callback,
dataType: "jsonp"
});
});
function success_callback(jqXHR, textStatus, errorThrown) {
alert('fail');
}
function error_callback(data, textStatus, jqXHR) {
alert('success');
}
});
</script>
</body>
</html>
当我尝试在 jQuery 中使用 oAuth 2.0 访问 salesforce.com 时,出现以下错误。
加载资源失败:服务器响应状态为 400 (Bad Request)
【问题讨论】:
-
您的代码没有什么特别的问题。 400 错误代码表示接收服务器不喜欢您发送的数据。检查 API 文档以了解它的预期内容和您发送的数据。
-
如果我尝试使用 apex 编程使用该服务,它工作正常。但根据我的要求,我必须在 JS/jQuery 中进行(仅限 clint 端编程)。
-
我刚刚注意到您将
Contact的函数引用设置为data属性。您是否打算发送一个对象? -
{ “的access_token”: “00D28000000IzNJ ARIAQOUTYHr3_fE_oWyhQgdF0i8KaUkxbXH4g.UUXmtSGU7EgV3Stjd3k2nBWUqQeEz_Qm_8b8ASXsTekTL7GsMT9BW2aq_d”, “instance_url”: “manoj6893-dev-ed.my.salesforce.com","id":"h…”} ==>这是格式我得到时,我打的服务。请告诉我对于 Contact 属性是否有任何替代方法
-
尝试发送
new Contact( .. ).toJsonString()
标签: jquery ajax web-services rest