Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I test XMLHttpRequest() in SDK with localhost?
    primarykey
    data
    text
    <p>The following code does not seem to work because when I try to get the "chooser" in Google App Engine (Python) it is undefined: </p> <pre><code>chooser = self.request.get("chooser") self.response.out.write("chooser: %s " % chooser) #returns "chooser:" without any value </code></pre> <p>Is this valid javascript?</p> <pre><code> var formData = new FormData(); formData.append("chooser", user); var xhr = new XMLHttpRequest(); //is it ok to test this with localhost? xhr.open("POST", "http://localhost:8086/g/choicehandler", true); xhr.onreadystatechange = function (aEvt) { if (xhr.readyState == 4 &amp;&amp; xhr.status == 200){ console.log("request 200-OK"); } else { console.log("connection error"); } }; xhr.send(formData); </code></pre> <p>Is the problem with the XHR call or with the App?</p> <p><strong>UPDATE</strong></p> <p>I am including the code in <code>/choice</code> to clarify what "chooser" is as per Daniel Roseman's comment:</p> <p>In <code>/choice</code> handler I have <code>writeToStorage()</code> which assigns a username in the form <code>user1, user2</code> and so on, and writes that to <code>localStorage</code>.</p> <p>After writing user name to <code>localStorage</code> I also need to write it to database in the app, and I use <code>xhr</code> to send it to <code>/g/choicehandler</code> handler.</p> <p>So, "chooser", I believe is a string, made of </p> <pre><code>var user = "user" + count; </code></pre> <p>I copy <code>/choice</code> handler below:</p> <pre><code>class Choice(webapp.RequestHandler): def get(self): self.response.out.write(""" &lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; var count = 0; function writeToStorage() { var user = "user" + count; count++; localStorage.setItem("chooser", user); var formData = new FormData(); formData.append("chooser", user); var xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:8086/g/choicehandler", true); xhr.onreadystatechange = function (aEvt) { if (xhr.readyState == 4 &amp;&amp; xhr.status == 200){ console.log("request 200-OK"); } else { console.log("connection error"); } }; xhr.send(formData); }; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form name="choice_form" id="choice_form" action="/g/choicehandler" method="post" onsubmit="writeToStorage()"&gt; &lt;textarea name="choice" rows="7" cols="50"&gt;&lt;/textarea&gt;&lt;br /&gt; &lt;input type="submit" value="submit your choice"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;""") </code></pre> <p><strong>UPDATE 2</strong></p> <p>I noticed in the logs that the text from textarea which is "choice" and "chooser" which is sent with xhr are not shown together, one of them is always without a value:</p> <pre><code>INFO ... chooser: user0 choice: INFO ... chooser: choice: abcd INFO ... chooser: user0 choice: INFO ... chooser: choice: efgh </code></pre> <p>This is the code for the above log:</p> <pre><code>chooser = self.request.get("chooser") choice = self.request.get("choice") logging.info("chooser: %s choice: %s" % tuple([chooser, choice])) new_choice = User( choice = choice, owner = chooser) new_choice.put() </code></pre> <p>so in the datastore i see "chooser" and "choice" written in 2 different rows. What am I doing wrong? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload