在尝试使用jquery ajax使用服务时无法获取JSON响应

时间:2015-12-22 11:15:44

标签: jquery ajax web-services rest

<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(错误请求)

1 个答案:

答案 0 :(得分:0)