菜鸟教程小白 发表于 2022-12-13 00:41:22

javascript - jQuery Ajax在cordova iOS应用程序上发送之前成功


                                            <p><p>以下摘录会更改登录按钮的 html,直到收到来自服务器的响应。</p>

<p>在使用 Cordova 构建的应用程序中在我的 iPhone 上进行测试时。在更新登录按钮的 html 之前触发警报。在桌面浏览器中,它按预期工作。</p>

<p>我尝试将缓存和异步设置为 false,但没有任何区别。</p>

<p>除了不同的 ajax 库,我想不出还有什么可以尝试的?</p>

<p>有没有更好的库可用于带有 cordova 的 AJAX?</p>

<pre><code>$.ajax({
    data: $data,
    cache: false,
    async: false,
    beforeSend: function() {
      $(&#39;#btn-login&#39;).html(&#39;Logging In...&#39;);
    },
    success: function(r, status) {
         if (r.status == &#39;success&#39;) {
             getUser();
             initNavSwipeGestures();
             $(&#39;#page-index&#39;).removeClass(&#39;active&#39;);
      } else {
            alert(r.message);
      }
    },
    error: function(data, status) {
      alert(status);
    }
});
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>成功时调用Success - <a href="http://api.jquery.com/jquery.ajax/" rel="noreferrer noopener nofollow">http://api.jquery.com/jquery.ajax/</a> :</p>

<blockquote>
<p>success
Type: Function( PlainObject data, String textStatus, jqXHR jqXHR )
A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.</p>
</blockquote>

<p>如果要检查返回的数据,请使用“成功”...否则应使用“完整”。</p>

<p>这样的事情应该可以工作:</p>

<pre><code>$.ajax({
    data: $data,
    cache: false,
    async: false,
    beforeSend: function() {
      $(&#39;#btn-login&#39;).html(&#39;Logging In...&#39;);
    },
    success: callback()
    },
    complete : function() {
         getUser();
         initNavSwipeGestures();
         $(&#39;#page-index&#39;).removeClass(&#39;active&#39;);      
    },
    error: function(data, status) {
      alert(status);
    }
});

var callback = function(response) {
   ///stuff to do after success here
};
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于javascript - jQuery Ajax在cordova iOS应用程序上发送之前成功,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/25264076/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/25264076/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - jQuery Ajax在cordova iOS应用程序上发送之前成功