10

jQuery Mobile 1.3.1 & jQuery JavaScript Library v1.9.1 を使用して PhoneGap アプリケーションを開発しています。ユーザー名とパスワードを使用したログインフォームがあります。ログインを処理するための JSON データを送受信できる JSON API があります。

ajax ログインには次の JavaScript を使用します。

$('#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");
        }
    });
});

ログインフォームの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>

しかし、これはうまくいきません。サーバーはログイン失敗エラーを返しますが、他のアプリでは正常に機能します。

4

3 に答える 3

7

あなたのコードにはいくつかのエラーがあります:

  1. 日付をデータに変更

    data       : {username : 'subin', password : 'passwordx'},
    
  2. この行を削除すると、Phonegap が正常に投稿されなくなります。

    xhrFields  : {withCredentials: true},
    
  3. beforeSend と complete で、この関数を使用してローダーを表示します。

    $.mobile.loading('show');
    

    $.mobile.loading('hide');
    

    あなたの現在のものはjQuery 1.2+で非推奨です。

最終的なコードは次のようになります。

$.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!');                  
    }
});     

CROSSドメインエラーが発生するため、サーバーからではなくディスクから実行してください。

<!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>   
于 2013-05-20T12:01:31.403 に答える
1

ここにタイプがあります:

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

これに変更してみてください:

    data       : {username : 'subin', password : 'passwordx'},
于 2013-05-20T11:48:51.307 に答える
0

データ型jsonpを使用

 dataType   : 'jsonp'

クロスドメインを有効にする

于 2015-11-07T20:55:03.940 に答える