Search code examples
jsonapixmlhttprequest

How can i solve the uncaught type error that comes when I create a function to handle my xhrhttp Requests?


I am practicing on how to use XHRHTTPRequest and i am creating a small library to handle the request but keep getting the errro below yet i cannot identify an error in the code.

below is my eay.js that has the library

function easyHTTP() {`enter code here`

    this.http = new XMLHttpRequest();


}


//make an HTTP POST REQUEST
easyHTTP.prototype.post = function (url, data, callback) {
    this.http.open('POST', url, true);
    this.http.setRequestHeader('Content-type', 'application/json');

    let self = this;
    this.http.onload = function () {
        callback(null, self.http.responseText)
    }

    this.http.send(JSON.stringify(data))

}

this is my main app.js where i use the libray

//Create Data
const data = {
    title: 'Custom Post',
    body: 'this is custo post'

};

//Create Post
http.post('https://jsonplaceholder.typicode.com/posts', data, function (err, post) {
    if (err) {
        console.log(err);

    } else {
        console.log(post);
    }
});

below is the html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>EasyHTTP Example</title>
</head>
<body>
  <h1>EasyHTTP Example</h1>

  <script src="easy.js"></script>
  <script src="newapp.js"></script>
</body>
</html>

i am making use of an external api to make the requests

And below is the error i get

main.js?attr=7N7be5KFTB_XK1v3646uVIL17y569qG6DwMTgbv_iXZvqSna9AM-9MNlqFPvJklSwn76uz-fcLWPIOcuFrAZuA:988 Uncaught TypeError: ns.GetCommandSrc is not a function
    at GetCommandUrl (main.js?attr=7N7be5KFTB_XK1v3646uVIL17y569qG6DwMTgbv_iXZvqSna9AM-9MNlqFPvJklSwn76uz-fcLWPIOcuFrAZuA:988)
    at XMLHttpRequest.window.XMLHttpRequest.open (main.js?attr=7N7be5KFTB_XK1v3646uVIL17y569qG6DwMTgbv_iXZvqSna9AM-9MNlqFPvJklSwn76uz-fcLWPIOcuFrAZuA:1023)
    at easyHTTP.post (easy.js:13)
    at newapp.js:23

Solution

  • I have the same problem. I have started to investigate on it and I have seen that the problem appear in a script that Kaspersky inject in each page. If you deactivate your Kapersky Antivirus, your page should work correctly.

    I still have no idea how to fix it with a proper solution. Maybe open a ticket to Kapersky support ?