Php 使用ajax将表单数据从Phonegap移动应用程序发送到REST CodeIgniter web服务
我正在编写一个phonegap移动应用程序,我想将数据从html表单发送到codeigniter web服务,但它不起作用。 这是我的表格代码:Php 使用ajax将表单数据从Phonegap移动应用程序发送到REST CodeIgniter web服务,php,ajax,web-services,codeigniter,cordova,Php,Ajax,Web Services,Codeigniter,Cordova,我正在编写一个phonegap移动应用程序,我想将数据从html表单发送到codeigniter web服务,但它不起作用。 这是我的表格代码: <form method="POST" action="" id="myform1"> <input type="text" id="descr" name="description" placeholder="Légende.."> <select id="select_file" name="sele
<form method="POST" action="" id="myform1">
<input type="text" id="descr" name="description" placeholder="Légende..">
<select id="select_file" name="select_file">
<option value="video">Vidéo</option>
<option value="son">Son</option>
<option value="image">Image</option>
<option value="autre">Autre</option>
</select>
<input type="file" id="myfile" name="my_file"/>
<button type="submit" id="publierFile" >Publier </button>
</form>
和我的php函数:
$('#myform1').submit(function(event) {
event.preventDefault();
$.post("mysite-url/webservices/mycontroller/addTest", $('form#myform1').serialize(), function(data) {
alert("hello");
console.log(data);
});
});
public function addTest_post() {
$desc = $this->post('description');
$this->response(array("description" => $desc), 201);
}
感谢id advance提供的帮助。您需要进行以下更改,请检查您的控制台是否存在错误:
1. $('#myform1').submit(function(event){ //add event in the arguments as you are using 'event.preventDefault();'
2. public function addTest_post(){ //change your function to post to accept post
请检查config.xml文件:根android项目/res/xml 添加这一行 示例代码
<widget xmlns = "http://www.w3.org/ns/widgets"
id = "io.cordova.helloCordova"
version = "2.0.0">
<name>Android Apps Cordova</name>
<description>
Android sample application.
</description>
<author email="chandrawardhana21@gmail.com">
Zyra-Studio
</author>
<access origin="*"/>
<!-- <content src="http://myserver.co/myapp.html" /> for external pages -->
<content src="index.html" />
Android应用程序Cordova
Android示例应用程序。
Zyra工作室
或
关于Access Control Allow Origin问题,我遇到了相同的错误,通过将这一行
放在项目根目录的index.php中解决了这个问题。在ur.submit函数中添加事件。$(“#myform1”).submit(函数(事件){已编辑。错误仍然存在。我认为您面临跨域请求的问题。请参阅此链接。它可能会解决您与跨域请求相关的问题。链接在此“我通过将jsonp
属性添加到$.post()解决了跨域问题;
它工作正常吗?仍然不工作。当我使用chrome的调试器时,我得到了这个策略错误,尽管我过去可以访问它:XMLHttpRequest无法加载。请求的资源上不存在“访问控制允许源代码”头。因此,不允许访问源代码“null”。