JavaScript中的XMLHttpRequest和基本功能

时间:2017-05-19 03:38:40

标签: javascript jquery json xmlhttprequest

我正在创建一个网站,该网站从网站接收用户名的高分,该网站根据网址中的用户名提供有组织的json数据。下面是我点击按钮并在名为RSname的变量中存储后从输入表单中获取用户名的javascript代码。按钮单击也执行函数getuserData(); getuserdata方法获取基本URL并将变量RSname连接到它的末尾。当我在测试期间点击按钮时,我收到一个错误说明。

  

XMLHttpRequest无法加载   https://www.tip.it/runescape/json/hiscore_user?old_stats=1&rsn=undefined。   请求中不存在“Access-Control-Allow-Origin”标头   资源。因此不允许来源“http://127.0.0.1:10315”   访问。

为什么说我的变量RSname未定义?请帮助我确定我可以解决这个问题,我只需要一些指导或跟踪。

var errormsg = "There was an ERROR I am sorry";

$(document).ready(function() {
    $('#retrievestats').click(function() {
        var enteredname = document.getElementById('Userinputform').value.toLowerCase();
        var RSname = enteredname;
        getUserData();
    });
});

function getUserData( RSname ) {
    var requestURL = "https://www.tip.it/runescape/json/hiscore_user?old_stats=1&rsn=", 
        request = new XMLHttpRequest();
    request.open( "GET", requestURL + encodeURIComponent( RSname ) );
    // various sanitizations should be employed on the backend when dealing with user input
    request.responseType = "json";
    request.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
    request.addEventListener( "readystatechange", function() {
        if ( request.readyState == 4 ) {
            if ( request.status == 200 ) {
                // process response
                var user_stats = request.response
                populateStatList();
            } else {
                alert(errormsg);
            }
        }
    }, false );
    request.send();
}

2 个答案:

答案 0 :(得分:0)

以下是问题:1)Cross Allow Origin将成为一个主要问题。由网站管理员来解决这个问题,而不是你。除了绕过系统之外,几乎没有什么可以做的,这并不容易。 2)正如@Titus在评论中所说,你不能简单地调用一个函数并期望传递一个变量。您必须将变量插入到函数中。

例如:

var myVariable = "Hi!";

function SlidinInYoDMs(myVariable){
  if(myVariable){
    //myVariable is inserted into the function. It is defined.
    var myElement = document.createElement('p');
    myElement.innerHTML = "This worked!";
    document.body.appendChild(myElement);
  }else{
  //myVariable is not inserted into the function. It isn't defined.
    var myElement = document.createElement('p');
    myElement.innerHTML = "This didn't work!";
    document.body.appendChild(myElement);
  
  }
}
SlidinInYoDMs(); //How you did it. Mind you, myVariable is already defined.
SlidinInYoDMs(myVariable); //How it's supposed to be done.

因此,在$(document).ready...代替getUserData()代替getUserData(RSname); myVariable

附注:在上面的示例中,var myVariable = "Hi!"; function SlidinInYoDMs(){ if(myVariable){ //myVariable is inserted into the function. It is defined. var myElement = document.createElement('p'); myElement.innerHTML = "This worked!"; myElement.innerHTML += "<br><br>myVariable = " + myVariable; document.body.appendChild(myElement); } } SlidinInYoDMs();已经定义。这是你可以解决你的小变量之谜的另一种方式。

getUserData(RSname);

答案 1 :(得分:0)

在ready函数中调用getUserData()时,将变量作为参数传递,如下所示:

{{1}}

这在编码方面非常基础。另外,为什么要在RSname中传输entername?无用

我希望它会对你有所帮助。