【问题标题】:AJAX request failing on mobile deviceAJAX 请求在移动设备上失败
【发布时间】:2013-05-17 13:36:55
【问题描述】:

如果我在浏览器中尝试以下代码,它可以工作,但是当我在我的设备 (Samsung Galaxy S3) 上尝试时,它不会完成 AJAX 请求。

AJAX 请求:

$.ajax({
    type: "POST",
    url: "http://somelink.nl/mobileajax/ajax.php",
    data: ({
        name: Username,
        pass: Pass,
        email: Email,
        firstname: FirstName,
        lastname: LastName
    }),
    cache: false,
    dataType: "text",
    success: alert('Succes')
});

希望大家能帮帮我!

编辑:

感谢您的帮助,但它仍然无法工作,发现不是 AJAX 请求出错,而是这里的其他内容是完整代码:

Javascript:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

function register()
    {

        var Username = $.trim($("#Username").val());
        var Pass = $.trim($("#Password").val());
        var Email = $.trim($("#Email").val());
        var FirstName = $.trim($("#FirstName").val());
        var LastName = $.trim($("#LastName").val());

        if((Username.length > 0) && (Pass.length > 0) && (Email.length > 0) && (FirstName.length > 0) && (LastName.length > 0) )
            {
            $.ajax({
                type: "POST",
                url: "http://quincycollijn.nl/mobileajax/ajax.php",
                data: ({name: Username, pass: Pass, email: Email, firstname: FirstName, lastname: LastName}),
                dataType: "text",
                success: onSucces,
                error: function(jq,status,message) {
                    alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message);
                }
            });
        } else {
            alert('Niks ingevult');
        } 


        function onSuccess()
            {
                alert('Result:');
            }
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>My Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <link rel="stylesheet" href="css/style.css" />
    <script src="phonegap.js"></script>
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
    <script src="js/index.js"></script>
  </head>
  <body onLoad="onBodyLoad()">

    <div data-role="page" data-theme="a">

      <div data-role="header">
        <h1>Chatup!</h1>
      </div><!-- /header -->

      <div data-role="content">
        <div data-role="fieldcontain">
        <img src="img/img_placeholder.png" class="placeholder" align="center" rel="Placeholder" >  
          <input class="login-input" data-mini="true" type="text" id="FirstName" name="first_name" placeholder="First Name" data-theme="b" >
        </br></br></br>
          <input class="login-input" data-mini="true" type="text" id="LastName" name="last_name" placeholder="Last Name" data-theme="b" >
        </br></br></br>
          <input type="text" data-mini="true" name="username" id="Username" placeholder="Username" data-theme="b" >
          </br>
          <input type="text" data-mini="true" name="email" id="Email" placeholder="Email" data-theme="b" >
          </br>
          <input type="password" data-mini="true" id="Password" name="password" placeholder="Password" data-theme="b" >
          </br>

          <a href="#" class="register-button" data-role="button" data-theme="b">Register</a>
        </div>
      </div><!-- /content -->
    </div><!-- /page -->

  </body>
</html>

【问题讨论】:

  • 你在你的 s3 上的其他浏览器上尝试过同样的方法吗?喜欢 Opera Mini?

标签: android ajax webrequest galaxy


【解决方案1】:

在第二行

我看到有一个不需要的单引号,可能会导致错误,是不是拼写错误?

【讨论】:

  • 你在 S3 上尝试过其他浏览器吗?
  • 并尝试 1. 添加错误处理程序 2. 根据 jquery 文档缓存(默认:true,对于 dataType 'script' 和 'jsonp' 为 false) 类型:Boolean 如果设置为 false,则将强制浏览器不缓存请求的页面。注意:将缓存设置为 false 仅适用于 HEAD 和 GET 请求。它通过将“_={timestamp}”附加到 GET 参数来工作。其他类型的请求不需要该参数,除非在 IE8 中对已由 GET 请求的 URL 进行 POST。
  • 感谢您的帮助,但它仍然无法正常工作我发现这不是 AJAX 请求出错而是其他问题,我现在将在问题中发布完整代码。
  • 如果你使用jquery mobile,不建议绑定普通的document.ready函数,你应该绑定mobileinit事件,更多请看jquerymobile.com/demos/1.2.0/docs/api/globalconfig.html
  • 对我来说看起来不错。只是随机检查 - 你确定代码中没有其他问题 - 比如 - 所有 # 控件都存在吗?很多时候,调用甚至可能无法到达 Ajax,但可能是拼写错误的控件名称或类名称。