2017-01-30 66 views
0

我正在尝试拨打AwaazDe REST API使用Javascript使用一些用户名和密码。但它不起作用。这里是我的代码使用Javascript使用用户名和密码调用REST API?

function authenticateUser(user, password) 
    { 
     var token = user + ":" + password; 

     // Should i be encoding this value????? does it matter??? 
     // Base64 Encoding -> btoa 
     var hash = btoa(token); 

     return "Basic " + hash; 
    } 

    /*function UserAction() {*/ 
    /* alert("Searching!");*/ 
    var xhttp = new XMLHttpRequest(); 
    xhttp.open("GET", "https://awaaz.de:443/console/streams-api/groups/", false); 
    xhttp.setRequestHeader("Content-type", "application/json"); 
    /*xhttp.setRequestHeader("username", "[email protected]"); 
    xhttp.setRequestHeader("password", "12345");*/ 
    xhttp.setRequestHeader("Authorization", authenticateUser("username","password")); 
    xhttp.send(); 
    var response = JSON.parse(xhttp.responseText); 
    alert("No response"); 
/*}*/ 

请帮我解决这个问题。任何帮助将不胜感激。

+0

不工作是非常模糊的词在您得到可能帮助的任何错误方面具体 –

+0

不工作意味着不使用使用REST API的GET方法获取内容。 –

+0

@SaurabhSinha打开浏览器调试器(NET)控制台并检查更多信息。它会节省您的时间 –

回答

0

它不工作,因为https://awaaz.de:443/不允许跨域请求 跨来源请求阻止:同源策略不允许在https://awaaz.de/console/streams-api/groups/读取远程资源。 (原因:缺少CORS头'Access-Control-Allow-Origin'

+0

任何解决方案?或者不可能使用Javascript调用AwaazDe API? –

+0

如果您的应用程序设置在同一台服务器上,完美工作。如果您需要在开发时测试和编码,请使用google chrome浏览器“Allow-Control-Allow-Origin:*”extension –

相关问题