Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Highlighted
Developer
Posts: 613
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use

PB - Ajax Post Request to Remote Server w/ jQuery

For WebWorks on PlayBook

I'm testing a way to send data to remote script which adds values into a mysql database. For this purpose, I am using jquery, and the following.

The AJAX post request doesn't work. Please comment if you see anything wrong with this code.

In my config.xml file

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns=" http://www.w3.org/ns/widgets"
        xmlns:rim="http://www.blackberry.com/ns/widgets"
        version="1.0.0.0">
    <name>My Application</name>
    <content src="index.html"/>
	<author>greenback</author>
	<rim:orientation mode="landscape" />
	<feature id="blackberry.ui.dialog"/>
	  <access uri="http://www.somedomain.com" subdomains="true">
	  </access>
    <icon src="icon.png"/>
</widget>

 The html file

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>onPlaybook</title>
    <style>
	body{margin:0;padding:0;}
	.wrap{width:1024px; height:600px;}
	</style>
	<script>
		document.ontouchmove = function(e) {
		e.preventDefault();
	}
	</script>
	<script src="jquery.js"></script>
	<script>
	$(function(){

         $("#form").submit(function(){
            var sv = $("#form").serialize();
            $.post('http://www.somedomain.com/add_hobby.php', sv, function(data){
               $("#content").html(data);
            });
            return false;
        });
});
</script>
	<meta name="viewport"
		content="width=device-width,
		initial-scale=1.0,
		maximum-scale=1.0,
		user-scalable=no">
  </head>
  <body>
     <div class="wrap">
    <div id="content"></div>
      <form id="form">
          Name <input type="text" name="name" /><br />
          Hobby<input type="text" name="hobby" /><Br />
          <input type="submit"/>
      </form>
	</div>
  </body>
</html>

 Please note I have included jquery locally.

add_hobby.php remote script

Basically processes post request from #form asynchronously, it should also insert data into my database. If successful returns "it works", if not it returns the words "fail" and adds them to #content

I uploaded add_hobby.php to my server. Then I packaged and deployed this app and tried running it in the simulator.

Outcome: The POST request did not work. Nothing was added to my database. If I test this locally, using Chrome, it works fine...

How do you do ajax post on in WebWorks?