Search code examples
javascriptxmlhttprequest

Testing XMLHttpRequest on local server


I have a xhr request. I'm trying testing code on my local server.

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 : '?'));
            }
        };
    }

I'm trying change response to my .json file on local server. But it doesn't work

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

Solution

  • I use it for testing on my local server

    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'));
        }
    };