Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Sending a POST request not working?

I am trying to send a POST request to a web services that uses and API. The service only allows POST requests so I tried:

var xhr = Ti.Network.createHTTPClient();
xhr.open('POST', 'http://www.example.com/api?foobar=true');

… but it is sending back an error stating that the request wasn't a post request. Is there something I am doing wrong or should I be looking to use a separate function?

— asked March 12th 2010 by Dana Woodman
  • api
  • createhttpclient
  • post
  • xhr
0 Comments

2 Answers

  • That code looks like (assuming you have a send after the xhr.open). We have a number of tests around POST requests so we're pretty sure it works fine. If you could provide more detail.

    — answered March 12th 2010 by Jeff Haynie
    permalink
    0 Comments
  • You have written
    xhr.open('POST', 'http://www.example.com/api?foobar=true');
    It should be
    xhr.open("POST", 'http://www.example.com/api?foobar=true');

    'POST' should be replaced as "POST"
    Rest every thing is fine.

    — answered May 22nd 2010 by Jagdeep Narang
    permalink
    2 Comments
    • Why do you have to replace the single quotes with double quotes?

      — commented September 3rd 2011 by Alex Casanova
    • hello im trying to execute a login authentification with android.i wanted to acces to mysql DB.
      this is my code
      var win = Titanium.UI.currentWindow;

      var username = Titanium.UI.createTextField({
      color:'#336699',
      top:10,
      left:10,
      width:300,
      height:40,
      hintText:'Username',
      keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
      returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
      borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
      });
      win.add(username);

      var password = Titanium.UI.createTextField({
      color:'#336699',
      top:60,
      left:10,
      width:300,
      height:40,
      hintText:'Password',
      passwordMask:true,
      keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
      returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
      borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
      });
      win.add(password);

      var loginBtn = Titanium.UI.createButton({
      title:'Login',
      top:110,
      width:90,
      height:35,
      borderRadius:1,
      font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14}
      });
      win.add(loginBtn);

      /*

      • Login Event Handling
        */
        var loginReq = Titanium.Network.createHTTPClient();
        loginReq.onload = function()
        {
        var json = this.responseText;
        var response = JSON.parse(json);
        if (response.logged == true)
        {
          alert("Welcome " + response.name + ". Your email is: " + response.email);
        
        }
        else
        {
          alert(response.message);
        
        }
        };

      loginReq.onerror = function()
      {
      alert("error");

      };

      /*

      • Login Button Click Event
        */

      loginBtn.addEventListener('click',function(e)
      {
      if (username.value != '' && password.value != '')
      {
      alert("hello");

          loginReq.open("POST",'http://localhost:80/larouse/post_auth.php');
          var params = {
              username: username.value,
              password: Ti.Utils.md5HexDigest(password.value)
          };
          loginReq.send(params);
      }
      else
      {
          alert("Username/Password are required");
      }
      

      });

      the file post_auth.php is located in the wamp server.
      wen i trying to run i occured error in the Titanium.Network.createHTTPClient();
      and the alert(error) also appear.what's the probleme help me right no plz.I'm so tired and so late in my project.I'm attending ur response
      thx

      — commented September 12th 2011 by Rajaa Samaha
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.