跳转ajaxjsphp
大家好 我现在准备用phonegap做一个安卓的app 现在需要做一个登录界面 有用户名密码 和提交按钮 当点击提交按钮 用ajax发送数据给 后台服务器 然后服务器返回true或者是false 如果是true跳入另外一个界面 如果是 false 弹出对话框提醒 现在问题是点击提交的时候一直显示ajax的error里面的 failed login。我觉得应该是url的问题 可是找了很久也不知道如何改:
ajax代码:
<
<code>script type="text/javascript"> $(document).ready(function() { $('#submit').click(function() { //Get data-theme data from all fields var username = $('#username').val(); var password = $('#password').val(); //Username and password can not be empty if(username =='') { alert('Please enter your username!'); return false; } if(password =='') { alert('Please enter your password!'); return false; } var form_data = { 'username': username, 'password': password }; //ajax code start here $.ajax ({ url: 'http://localhost/restserver/index.php/api/apiauth/auth', <!--//for test need to consolehange IP address here--> data: form_data, type: 'POST', cache: false, dataType: "json", error: function(XMLHttpRequest, textStatus, errorThrown) { alert('Failed to login'); console.log(JSON.stringify(XMLHttpRequest)); console.log(JSON.stringify(textStatus)); console.log(JSON.stringify(errorThrown)); }, success: function (data) { alert("Success login"); //$.mobile.changePage("index.html", "slideup"); } }); return false; }); }); </script></p> <p>html部分:</p> <pre class="brush:php;toolbar:false"><code> <div> <label for="username">Username</label> <input name="username" type="text" id="username" placeholder="Username"/> </div> <div> <label for="password">password</label> <input name="password" type="password" id="password" placeholder="Password"/> </div> <fieldset> <div><button name="submit" type="button" data-theme="b" id="submit">Login</button> </div> </fieldset> </div></code>
后台RESTful CI:
<code><?php defined('BASEPATH') OR exit('No direct script access allowed');require APPPATH.'/libraries/REST_Controller.php';class Apiauth extends REST_Controller{ function auth_get() { $this->load->model('membership_model'); $query=$this->membership_model->validate(); $this->response($query,200); }}<?phpclass Membership_model extends CI_Model{ function validate() { $this->db->where('username',$this->input->post('username')); $this->db->where('password',$this->input->post('username')); $query=$this->db->get('membership'); if($query->num_rows=1) { return true; } else { return false; } }}</code>