在PhoneGap中使用AJAX/JSP进行身份验证


 <form method="POST" class="userform" id="loginForm">
    <div data-role="content">
      <h2> Login:</h2>
        <div data-role="fieldcontain">
            <input name="email" placeholder="put your name" type="email" data-mini="true">
        </div>
        <div data-role="fieldcontain">
            <input name="password" placeholder="enter your password" type="password" data-mini="true">
        </div>
        <input type="submit" data-theme="a" value="submit" id="submitButton">
        <h5 align="center">
            <a href="user_rbooks.html"> Forget password? </a>
        </h5>
        </div>
        </form>

这是我的登录

$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
};
var $bro = $('#loginForm');
$('#submitButton').click(function(e) {
        //console.log("submit button has been clicked");
        e.preventDefault(); //cancel form submit
        var jsObj = $bro.serializeObject()
            , ajaxObj = {};
        //console.log(jsObj);
        ajaxObj = {  
            type: "POST",
            url: "http://192.168.0.100:8080/AdvancedLibrarySystem/api/v1/login", 
            data: JSON.stringify(jsObj), 
            contentType:"application/json",
            error: function(jqXHR, textStatus, errorThrown) {
                console.log("Error " + jqXHR.getAllResponseHeaders() + " " + errorThrown);
            },
            success: function(data) { 
                console.log(data);
                if(data[0].status == '200') {
                    alert("Welcome!!!")
                    $('#div_ajaxResponse').text( data[0] );
                    $.mobile.changePage('home.html');
                }
                else{
                alert("Incorret Username or Password!!!")
                }
            },
            complete: function(XMLHttpRequest) {
                //console.log( XMLHttpRequest.getAllResponseHeaders() );
            }, 
            dataType: "json" //request JSON
        };
        $.ajax(ajaxObj);
    });

我正在尝试通过Ajax在PhoneGap中使用身份验证,但是如果我试图在Chrome控制台中运行该代码,则可以正常工作,但是当我在PhoneGap应用程序中使用它时,它不会在服务器中给出响应。..任何人都可以帮我...

我不确定您的意思是'不给予服务器中的响应',但是值得一看的一件事是phonegap项目的config.xml中的跨域选项。那里应该有一个"访问"标签,指定可以访问哪些域。尝试将其设置为"*",然后重试。

最新更新