none
可以用JS去调用Bing Search API RRS feed

  • 问题

  • 我想用javascript去调用Bing Search API, 但是总是报认证错误,是不能使用Js调用呢还是我的认证方式错了?
    2015年2月22日 6:13

答案

  • 您好,

    JS可以调用BIng API,但验证方式可能有些改变,您需要使用Basic的方式验证,您可以参考如下代码:

    <!DOCTYPE html>
    <html>
    <head>
        <script src="encode.js"></script>
        <script type="text/javascript">
            function loadXMLDoc()
            {
                var xmlhttp;
                if (window.XMLHttpRequest)  
                {
                    // code for IE7+, Firefox, Chrome, Opera, Safari  
                    xmlhttp=new XMLHttpRequest();  
                }else  {
                    // code for IE6, IE5  
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  
                }
                xmlhttp.onreadystatechange=function()  
                {  
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                    } else
                    {
                        document.getElementById("myDiv").innerHTML = xmlhttp.responseText+"state text:"+xmlhttp.statusText;
                    }
                }
                var requestStr = "https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=%27willtest%27&$format=json";
                var requestStr2 = "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query=%27will%27&$top=10&$format=JSON";
                xmlhttp.open("Get", requestStr, true);
                xmlhttp.setRequestHeader('Authorization', 'Basic ' + btoa(':yourkey));
                xmlhttp.send();
            }</script>
    </head>
    <body>
        <h2>Bing Test</h2><button type="button" onclick="loadXMLDoc()">Request data</button><div id="myDiv"></div>
    </body>
    </html>

    希望能够帮到您。

    Regards,

    Will

    如果您想进一步了解Windows Azure, Windows Azure 官网欢迎您的访问


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    2015年2月22日 6:42
    版主

全部回复

  • 您好,

    JS可以调用BIng API,但验证方式可能有些改变,您需要使用Basic的方式验证,您可以参考如下代码:

    <!DOCTYPE html>
    <html>
    <head>
        <script src="encode.js"></script>
        <script type="text/javascript">
            function loadXMLDoc()
            {
                var xmlhttp;
                if (window.XMLHttpRequest)  
                {
                    // code for IE7+, Firefox, Chrome, Opera, Safari  
                    xmlhttp=new XMLHttpRequest();  
                }else  {
                    // code for IE6, IE5  
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  
                }
                xmlhttp.onreadystatechange=function()  
                {  
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
                    } else
                    {
                        document.getElementById("myDiv").innerHTML = xmlhttp.responseText+"state text:"+xmlhttp.statusText;
                    }
                }
                var requestStr = "https://api.datamarket.azure.com/Bing/SearchWeb/v1/Web?Query=%27willtest%27&$format=json";
                var requestStr2 = "https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/v1/Web?Query=%27will%27&$top=10&$format=JSON";
                xmlhttp.open("Get", requestStr, true);
                xmlhttp.setRequestHeader('Authorization', 'Basic ' + btoa(':yourkey));
                xmlhttp.send();
            }</script>
    </head>
    <body>
        <h2>Bing Test</h2><button type="button" onclick="loadXMLDoc()">Request data</button><div id="myDiv"></div>
    </body>
    </html>

    希望能够帮到您。

    Regards,

    Will

    如果您想进一步了解Windows Azure, Windows Azure 官网欢迎您的访问


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    2015年2月22日 6:42
    版主
  • 非常感谢您的说明!我之前验证方式的代码写的有误。多谢纠正
    2015年2月22日 14:50