XMLHttpRequest的send方法在Javascript

时间:2018-03-13 01:29:44

标签: paypal internet-explorer-11

我一直在关注一些文章来解决这个问题,但都没有奏效。我有一个使用XMLHttpRequest调用paypal api的javascript函数。此功能适用于Firefox / Chrome,但在IE 11中失败(虽然我没有尝试过以前的IE版本)。使用IE 11,在xhr.send()行;我得到“网络错误”,然后它就死了。在控制台中,只要在网络呼叫之前加载页面,我就可以看到,

SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.

以下是一个javascript函数,我打电话从paypal获取访问令牌。

 function getAccessToken(strClientID,strSecretKey){
        try
            {
                //***********************code for access token call starts***************************
                var strbody = 'grant_type=client_credentials';
                var xmlhttp = new XMLHttpRequest();
                xmlhttp.open('POST', 'https://api.sandbox.paypal.com/v1/oauth2/token', false);
                xmlhttp.setRequestHeader("Content-Type", "application/x-
                www-form-urlencoded");
                xmlhttp.setRequestHeader('Authorization', 'Basic ' + window.btoa(strClientID+':'+strSecretKey)); //in prod, you should encrypt user name and password and provide encrypted keys here instead
                xmlhttp.send(strbody);
                var response = xmlhttp.responseText;
                var obj = JSON.parse(response);
                var access_token = obj.access_token; 
                return access_token;
                //***********************code for access token call ends***************************
            }
            catch(err){

                alert(err.message);
            }
    } 

我一直在关注this这些建议使用CORS的帖子。所以,我已经尝试了提到here的函数createCORSRequest(method,url),但仍然遇到同样的错误。

后来,我想到使用JQuery而不是Javascript,我将上面的函数转换为ajax调用

                    $.ajax({

                            method:"POST",
                            url : "https://api.sandbox.paypal.com/v1/oauth2/token",
                            data:{"grant_type":"client_credentials"},
                            datatype:'json', 
                            async: false,  
                            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',                                
                            headers: {
                                "Authorization": "Basic " + btoa(client_id+":"+strKey)
                            },
                            success: function(response) {

                                alert('Ajax call successfull !!');

                            },
                            error: function(XMLHttpRequest,status,error) {
                                alert('Ajax call NOT successfull !!'+error);
                            }

                        });

但是这个ajax调用仍在打印“Ajax call successfull !!”对于firefox / chrome和IE 11,我得到了     “Ajax调用not successfull !! NetworkError”。

here如果浏览器是IE浏览器,我读到了关于使用ActiveXObject ...仍然没有运气..

我不确定我是朝着正确的方向,还是只是感到困惑......只是为了提醒它在Firefox和Chrome中运行良好,但在IE 11中运行不正常。

请帮助。

感谢

0 个答案:

没有答案