发送和接收JSON数据| PhoneGap Jquery手机应用程序使用ajax()

前端之家收集整理的这篇文章主要介绍了发送和接收JSON数据| PhoneGap Jquery手机应用程序使用ajax()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个PhoneGap应用程序与jQuery Mobile 1.3.1& jQuery JavaScript Library v1.9.1.我有一个用户名和密码的登录表单.有一个JSON api可以发送和接收JSON数据来处理登录.

我使用以下JavaScript进行ajax登录.

  1. $('#submit').bind('click',function(e) {
  2. e.preventDefault();
  3. $.ajax({
  4. type : "POST",url : "http://domainx/public/login",xhrFields : {withCredentials: true},crossDomain: true,beforeSend : function() {$.mobile.showPageLoadingMsg();},complete : function() {$.mobile.hidePageLoadingMsg();},data : {username : 'subin',password : 'passwordx'},dataType : 'json',success : function(response) {
  5. console.error(JSON.stringify(response));
  6. },error : function() {
  7. console.error("error");
  8. }
  9. });
  10. });

这是我的登录表单的HTML代码.

  1. <div data-role="content">
  2. <form id="login-form" data-ajax="false" method="post">
  3. <fieldset>
  4. <input type="text" name="username" id="username" value="subin" placeholder="Username">
  5. <input type="password" name="password" id="password" value="passwordx" placeholder="Password">
  6. <input type="button" data-theme="b" name="submit" id="submit" value="Enter" data-icon="plus">
  7. </fieldset>
  8. </form>
  9. </div>

但这只是不行.服务器返回登录失败错误,虽然它适用于其他应用程序.

解决方法

你有几个错误是你的代码

>将日期更改为数据

  1. data : {username : 'subin',

>删除此行,它将阻止Phonegap成功发布:

  1. xhrFields : {withCredentials: true},

>在你的beforeSend并完成使用这个功能显示加载器:

  1. $.mobile.loading('show');

  1. $.mobile.loading('hide');

您当前的这些在jQuery 1.2中已被弃用.

您的最终代码应如下所示:

  1. $.ajax({
  2. type : "POST",url : "http://domain/public/login",beforeSend : function() {$.mobile.loading('show')},complete : function() {$.mobile.loading('hide')},success : function(response) {
  3. //console.error(JSON.stringify(response));
  4. alert('Works!');
  5. },error : function() {
  6. //console.error("error");
  7. alert('Now working!');
  8. }
  9. });

证明它正在工作,只是从磁盘运行,而不是从服务器,因为你会得到一个CROSS域错误

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>jQM Complex Demo</title>
  5. <Meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  6. <Meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
  7. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
  8. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  9. <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
  10. <script>
  11. $.ajax({
  12. type : "POST",success : function(response) {
  13. //console.error(JSON.stringify(response));
  14. alert('Works!');
  15. },error : function() {
  16. //console.error("error");
  17. alert('Not working!');
  18. }
  19. });
  20. </script>
  21. </head>
  22. <body>
  23. <div data-role="page" id="index">
  24. <div data-theme="b" data-role="header">
  25. <h1>Index page</h1>
  26. </div>
  27.  
  28. <div data-role="content">
  29.  
  30. </div>
  31. </div>
  32. </body>
  33. </html>

猜你在找的Ajax相关文章