我创建了一个 REST Web 服务,在其中提取姓名和姓氏,然后简单地发回一条消息,其中包含一条消息,结果中包含姓名和姓氏。
现在在 PhoneGap 我有以下代码:
<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximumscale=1"/>
<link rel="stylesheet" href="css/jquery.mobile.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquerym.js"></script>
<script type="text/javascript" src="js/myscript.js"></script>
<script type="text/javascript" charset="utf-8" src="js/cordova-2.3.0.js"></script>
</head>
<body>
<div id ="page1" data-role="page">
<div data-role ="header">
<h1>Welcome to Page 1</h1>
</div>
<a href="#page2" data-transition="flip">Page2</a>
</div>
<div id ="page2" data-role="page">
<div data-role ="header">
<h1>Welcome to Page 2</h1>
</div>
<form>
First name: <input type="text" name="name" id="username"><br>
Last name: <input type="text" name="surname" id="usersurname"><br>
<input type="submit" id ="submit" name="submit" value="GO TIME">
</form>
<a href="#page1" data-transition="flip">Page1</a>
</div>
</body>
</html>
当用户在表单中输入值时,它会运行以下 js:
$(document).on('pagebeforeshow','#page2',
function(){
$('#submit').click(function()
{
var name = $("#username").val();
var surname = $("#usersurname").val();
alert(name + " " + surname);
$.post(
"http://localhost/rest/index.php/api/test/name/"+name+"/surname/"+surname+"/format/json",
{'name':name,'surname':surname},
function(data)
{
alert(data.result);
},
"json");
});
});
在我的 Codeigniter REST 中,我设置了基本 url 并具有以下代码:
<?php
require(APPPATH.'/libraries/REST_Controller.php');
class API extends REST_Controller
{
function test_post()
{
$name = $this->post('name');
$surname = $this->post('surname');
$result = "This is working ".$name." ".$surname;
$array = array('result' => $result);
$this->response($array);
}
}
?>
现在,当用户在输入框中输入他们的信息然后单击按钮时,js 脚本工作正常,因为出现了第一个警报,但由于某种原因,它没有转到 codeigniter REST 并返回结果,它只是将我发送回第 1 页。