Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

UnityWebRequest.Post in Unity Tiny?

Discussion in 'Project Tiny' started by nimauke, May 14, 2019.

  1. nimauke

    nimauke

    Joined:
    May 18, 2018
    Posts:
    7
    Is there something similar to UnityWebRequest.Post in Unity Tiny that I can use to update a variable in a database?
    Thank you
     
  2. Zionisias

    Zionisias

    Joined:
    Apr 23, 2019
    Posts:
    40
  3. nimauke

    nimauke

    Joined:
    May 18, 2018
    Posts:
    7
    Thank you for your answer!
    I gave it a try, but it only reads the value of the variable on my database and doesn't change it.

    Code (JavaScript):
    1.  var http = new XMLHttpRequest();
    2.             var url = 'myurl.php';
    3.             var params = '10';
    4.             http.open('POST', url, true);
    5.  
    6.             http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    7.            
    8.             http.onreadystatechange = function() {//Call a function when the state changes.
    9.             if(http.readyState == 4 && http.status == 200) {
    10.             console.log(http.responseText);
    11.                 }
    12.             }
    13.             http.send('add_score = 20');
    Am i doing something wrong?
     
  4. Zionisias

    Zionisias

    Joined:
    Apr 23, 2019
    Posts:
    40
    Hmm, I'm not sure how to directly change a value in the database via an XMLHttpRequest, as the post-request is only meant to be used to sent data. Normally you would have a post-call in your myurl.php, which should be triggered by running the code you provided above. There you should be able to read the data which you have sent by http.send() and use this data to update your database.

    I haven't really worked on the server side of handling web calls for a while, so if anybody can step in to provide more info, feel free ;).
     
    reallyhexln likes this.
  5. nimauke

    nimauke

    Joined:
    May 18, 2018
    Posts:
    7
    I think I figured it out now. Changing the last row to
    Code (JavaScript):
    1.   http.send('add_score= '+20);
    seems to work.
    Thank you for your help :)
     
    Zionisias likes this.
  6. reallyhexln

    reallyhexln

    Joined:
    Jun 18, 2018
    Posts:
    69
    nimauke, to avoid such issues in the future you can try to use FormData object passed to send() method:
    Code (JavaScript):
    1. var formData = new FormData();
    2. formData.append("add_score", 20);
    3. http.send(formData);
     
    nimauke and Zionisias like this.
  7. nimauke

    nimauke

    Joined:
    May 18, 2018
    Posts:
    7