Using AJAX with JQuery Mobile

4 篇文章 0 订阅
2 篇文章 0 订阅
 

Using AJAX with JQuery Mobile

Post to Twitter

JQuery Mobile is a rapidly growing framework for smartphones and tablets and is gaining a lot of developer attention. The nice thing about it is we can also use all the power of JQuery along with it. Today I’ll cover a simple example that uses JQuery Mobile and PHP to send some data to the server and get a personalized message back. A “Hello World JQuery Mobile with PHP” if you will.

I have a previous JQuery Mobile tutorial that uses Ajax so you can also refer to the code in there. The difference isthat article uses a form, this one doesn’t.


Update – Nov. 11, 2010: Code updated to reflect changes in JQuery Mobile Alpha 2.

To get started I had a local web server and created a folder to put my files. I used Apache 2 but feel free to use anything that can run PHP. The PHP code is dead simple:

<?php
     echo "Hello " . $_REQUEST [ "name" ];
?>

Save that in a file called callajax.php.

Note: Using Ajax in JQuery is simple, see the docs for more information.

The full code for the index.html file is as follows:

<!DOCTYPE html>
< html >
     < head >
     < title >JQuery Mobile AJAX</ title >
     < link rel = "stylesheet" href = "http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.css" />
     < script src = "http://code.jquery.com/jquery-1.4.4.min.js" ></ script >
     < script src = "http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js" ></ script >
</ head >
  
< body >
  
     < script >
         $(function() {
  
             $("#callAjax").click(function() {
                 var theName = $.trim($("#theName").val());
  
                 if(theName.length > 0)
                 {
                     $.ajax({
                       type: "POST",
                       url: "http://10.0.2.2/mobileajax/callajax.php",
                       data: ({name: theName}),
                       cache: false,
                       dataType: "text",
                       success: onSuccess
                     });
                 }
             });
  
             $("#resultLog").ajaxError(function(event, request, settings, exception) {
               $("#resultLog").html("Error Calling: " + settings.url + "< br />HTPP Code: " + request.status);
             });
  
             function onSuccess(data)
             {
                 $("#resultLog").html("Result: " + data);
             }
  
         });
     </ script >
  
     < div data-role = "page" id = "indexPage" >
         < div data-role = "header" >
             < h1 >JQuery Mobile</ h1 >
         </ div >
         < div data-role = "content" >
             < div data-role = "fieldcontain" >
                 < label for = "theName" >Please enter your name:</ label >
                 < input type = "text" id = "theName" name = "theName" value = "" />
             </ div >
             < input id = "callAjax" type = "button" value = "Call Ajax" />
             < div id = "resultLog" ></ div >
         </ div >
  
         < div data-role = "footer" >
             < h1 >AJAX Demo</ h1 >
         </ div >
     </ div >
  
</ body >
</ html >

A few things to note:

- The call to the server uses this address: http://10.0.2.2/mobileajax/callajax.php
This is because when I use the Android emulator to run this web app it doesn’t currently support “localhost” so you need to use the10.0.2.2 address instead.

- I’m simply passing the value of the textbox to the PHP script (obviously you need some real security here in a production app as you should never trust data from the client). PHP processes this data and sends the reply back. If an error occurs in the Ajax call I capture that and print some data out.

Here are a couple screenshots:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值