2017-01-23 144 views
0

我有一个xhr请求。我正在尝试在本地服务器上测试代码。在本地服务器上测试XMLHttpRequest

GetMetaData: function() { 
     var xhr = new XMLHttpRequest(); 
     xhr.open("POST", "../?/?.asmx/GetData", true); 
     xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8;'); 
     xhr.send(JSON.stringify({ 
      "args": JSON.stringify({ 
       method: "?", 
       factors: "?", 
       lcid: app.ResponseParamsValues.lcid, 
      }) 
     })); 

     xhr.onreadystatechange = function() { 
      if (this.readyState == 4) { 
       var response = (JSON.parse(this.responseText).d); 
       app.variables.metaValues = response.Result; 
      } 
      if (this.status != 200) { 
       console.log('error: ' + (this.status ? this.statusText : '?')); 
      } 
     }; 
    } 

我试图改变对本地服务器上的.json文件的响应。但是,这是行不通的

GetMetaData: function() { 
     return "../../data/data.json"; 
} 
+0

大的。但这不是一个问题。 – Xufox

回答

0

我用它来测试我的本地服务器

var xhr = new XMLHttpRequest(); 
xhr.open("GET", "example.json", true); 
xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8;'); 
xhr.send(); 

xhr.onreadystatechange = function() { 

    if (this.readyState == 4) { 
      var response = JSON.parse(this.responseText); 

      //some actions with the response 
    } 

    if (this.status != 200) { 
      console.log('error: ' + (this.status ? this.statusText : 'request failed')); 
    } 
}; 
相关问题