使用ajax()发送和接收JSON数据| PhoneGap + Jquery移动应用

时间:2022-09-25 21:32:35

I'm developing a PhoneGap application with jQuery Mobile 1.3.1 & jQuery JavaScript Library v1.9.1. I have a login form with a username and a password. There is a JSON api which can send and receive JSON data for processing the login.

我正在开发一个带有jQuery Mobile 1.3.1和jQuery JavaScript库v1.9.1的PhoneGap应用程序。我有一个用户名和密码的登录表单。有一个JSON api可以发送和接收JSON数据以处理登录。

I use the following JavaScript for the ajax login.

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

$('#submit').bind('click', function(e)  {
    e.preventDefault();
    $.ajax({
        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) {
            console.error(JSON.stringify(response));
        },
        error      : function() {
            console.error("error");
        }
    });
});

Here is my html code for the login form.

这是我的登录表单的html代码。

<div data-role="content">
     <form id="login-form" data-ajax="false" method="post">
        <fieldset>
           <input type="text" name="username" id="username" value="subin" placeholder="Username">
           <input type="password" name="password" id="password" value="passwordx" placeholder="Password">
           <input type="button" data-theme="b" name="submit" id="submit" value="Enter" data-icon="plus">
        </fieldset>
     </form>
  </div>

But this just don't work. The server returns login failure error, although it works fine for other apps.

但这是行不通的。服务器返回登录失败错误,尽管它对其他应用程序很好用。

3 个解决方案

#1


7  

You have several error is your code:

您有几个错误是您的代码:

  1. Change date to data

    更改日期的数据

    data       : {username : 'subin', password : 'passwordx'},
    
  2. Remove this line, it will prevent Phonegap from successful posting:

    删除这一行,将阻止Phonegap成功发布:

    xhrFields  : {withCredentials: true},
    
  3. In your beforeSend and complete use this function to show loader:

    在您的上述和完整中,使用此功能显示加载程序:

    $.mobile.loading('show');
    

    and

    $.mobile.loading('hide');
    

    your current ones are deprecated in jQuery 1.2+.

    您当前的版本在jQuery 1.2+中被弃用。

Your final code should look like this:

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

$.ajax({
    type       : "POST",
    url        : "http://domain/public/login",
    crossDomain: true,
    beforeSend : function() {$.mobile.loading('show')},
    complete   : function() {$.mobile.loading('hide')},
    data       : {username : 'subin', password : 'passwordx'},
    dataType   : 'json',
    success    : function(response) {
        //console.error(JSON.stringify(response));
        alert('Works!');
    },
    error      : function() {
        //console.error("error");
        alert('Now working!');                  
    }
});     

Proof that it is working, just run it from the disk and not from a server because you will get a CROSS domain error:

证明它正在工作,只需从磁盘而不是服务器运行它,因为您将得到一个跨域错误:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <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"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>          
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
        <script>
    $.ajax({
        type       : "POST",
        url        : "http://domain/public/login",
        crossDomain: true,
        beforeSend : function() {$.mobile.loading('show')},
        complete   : function() {$.mobile.loading('hide')},
        data       : {username : 'subin', password : 'passwordx'},
        dataType   : 'json',
        success    : function(response) {
            //console.error(JSON.stringify(response));
            alert('Works!');
        },
        error      : function() {
            //console.error("error");
            alert('Not working!');                  
        }
    });     
        </script>
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>

            <div data-role="content">

            </div>
        </div>    
    </body>
</html>   

#2


1  

You have a type here:

你有一个类型:

    date       : {username : 'subin', password : 'passwordx'},
//--^^^^----this should be named data

try changing to this:

试着改变:

    data       : {username : 'subin', password : 'passwordx'},

#3


0  

use datatype jsonp

数据类型使用jsonp

 dataType   : 'jsonp'

enables cross domain

支持跨域

#1


7  

You have several error is your code:

您有几个错误是您的代码:

  1. Change date to data

    更改日期的数据

    data       : {username : 'subin', password : 'passwordx'},
    
  2. Remove this line, it will prevent Phonegap from successful posting:

    删除这一行,将阻止Phonegap成功发布:

    xhrFields  : {withCredentials: true},
    
  3. In your beforeSend and complete use this function to show loader:

    在您的上述和完整中,使用此功能显示加载程序:

    $.mobile.loading('show');
    

    and

    $.mobile.loading('hide');
    

    your current ones are deprecated in jQuery 1.2+.

    您当前的版本在jQuery 1.2+中被弃用。

Your final code should look like this:

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

$.ajax({
    type       : "POST",
    url        : "http://domain/public/login",
    crossDomain: true,
    beforeSend : function() {$.mobile.loading('show')},
    complete   : function() {$.mobile.loading('hide')},
    data       : {username : 'subin', password : 'passwordx'},
    dataType   : 'json',
    success    : function(response) {
        //console.error(JSON.stringify(response));
        alert('Works!');
    },
    error      : function() {
        //console.error("error");
        alert('Now working!');                  
    }
});     

Proof that it is working, just run it from the disk and not from a server because you will get a CROSS domain error:

证明它正在工作,只需从磁盘而不是服务器运行它,因为您将得到一个跨域错误:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <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"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>          
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
        <script>
    $.ajax({
        type       : "POST",
        url        : "http://domain/public/login",
        crossDomain: true,
        beforeSend : function() {$.mobile.loading('show')},
        complete   : function() {$.mobile.loading('hide')},
        data       : {username : 'subin', password : 'passwordx'},
        dataType   : 'json',
        success    : function(response) {
            //console.error(JSON.stringify(response));
            alert('Works!');
        },
        error      : function() {
            //console.error("error");
            alert('Not working!');                  
        }
    });     
        </script>
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>

            <div data-role="content">

            </div>
        </div>    
    </body>
</html>   

#2


1  

You have a type here:

你有一个类型:

    date       : {username : 'subin', password : 'passwordx'},
//--^^^^----this should be named data

try changing to this:

试着改变:

    data       : {username : 'subin', password : 'passwordx'},

#3


0  

use datatype jsonp

数据类型使用jsonp

 dataType   : 'jsonp'

enables cross domain

支持跨域