1

hi I wrote simple AJAX call to php file I am getting error in my console . Request should go to the server and return back in the html input field . i am not able to solve Below is my Code

HTML:

    <label>Input your text: </label><input type="text" id="user_text"   onkeyup="update1()"/></br></br>
    <label>Response here: </label><input type="text" id="server_response"  readonly/>

Javascript:

    function update1(){
var current_text = document.getElementById("user_text").value;
var http = new XMLHttpRequest();
http.onreadystatechange = function(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        document.getElementById('server_response').value = response;
    }
    http.open("GET" , "http://localhost/inderstand_Ajax/server.php?user_text="+current_text , true);
    http.send();
}

};

PHP:

    <?php
    $data = $_GET;
    $user_text = $_GET['user_text'];
    $response = strtoupper($user_text)
    echo $response;
    ?>

Error in the console :

    Uncaught (in promise) Object {message: "Invalid method piSession.buildPageInteractionSession", stack: "Error: Invalid method piSession.buildPageInteracti…on↵    at true-key://data/scripts/core.js:10:8889"}
NaveenKG
  • 41
  • 2
  • 11

3 Answers3

1

Your code is wrong.

It should be:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       // Typical action to be performed when the document is ready:
       document.getElementById("server_response").innerHTML = xhttp.responseText;
    }
};
xhttp.open("GET", "http://localhost/inderstand_Ajax/server.php?user_text="+current_text, true);
xhttp.send(); 

and not:

http.onreadystatechange = function(){
    if(http.readyState == 4 && http.status == 200){
        var response = http.responseText;
        document.getElementById('server_response').value = response;
    }
    http.open("GET" , "http://localhost/inderstand_Ajax/server.php?user_text="+current_text , true);
    http.send();
}

you cannot send the request inside onreadystatechange.

codtex
  • 6,128
  • 2
  • 17
  • 34
  • I tried changing it , same error still , and I have copied it from a tutorial and his tutorial example is working fine . is it anything related to configuration . var xhttp or http both are same rite ? – NaveenKG Mar 04 '17 at 09:33
  • When you changed the javascript try removing browser cache to be sure that javascript is reloaded. You should not see this error any more with the above code. Probably another error but not this one. Can you provide the link of this tutorial – codtex Mar 04 '17 at 13:50
  • @NaveenKG you can force the browser to reload javascript when removing history from last hour – codtex Mar 04 '17 at 13:51
  • Somehow the code worked , but in the console that error is still there – NaveenKG Mar 04 '17 at 13:51
  • 1
    This error tells us that there was error in Promise which was not handled. But the problem is that I don't see any promise in your code snippet. Do you have any other javascript in your sample, I think this error is coming from somewhere else and not the provided code snippet – codtex Mar 04 '17 at 14:06
  • 1
    @NaveenKG I did test the code and is working fine and you should also change `document.getElementById("server_response").innerHTML` to `document.getElementById("server_response").value` if you want to see the response or change `` to something like `
    `. This error is not related to this code snippet man, there should be something else causing the error check this post http://stackoverflow.com/a/37628027/5452965 - this is causing the error
    – codtex Mar 04 '17 at 14:19
0

This problem occurred also for me today. I removed true-key from my chrome-browser and now the error in the console is gone. Try remove true-key from your chrome-browser and tell if the error still occurs in the console.

Ilan M
  • 299
  • 3
  • 8
  • @llan M you are rite , I was having the instinct that my Apache server or chrome is having the problem , cos even after I close that project I still see the error in my console , I will try to remove as u mentioned thank you – NaveenKG Mar 05 '17 at 20:33
0

This is a problem with the latest version (at time of writing) of the True Key chrome extension.

Mike Haas
  • 2,273
  • 3
  • 25
  • 30