0

I'm programming a phonegap mobile app and I want to send data from my html form to my codeigniter web service but it's not working. Here is my form code :

<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>

my ajax function :

$('#myform1').submit(function(event) {
    event.preventDefault();
    $.post("mysite-url/webservices/mycontroller/addTest", $('form#myform1').serialize(), function(data) {
        alert("hello");
        console.log(data);
    });

});

and my php function :

public function addTest_post() {
    $desc = $this->post('description');
    $this->response(array("description" => $desc), 201);
}

Thanks id advance for your help.

  • add event in ur .submit function.$('#myform1').submit(function(event){ – Abhijit Mali Apr 22 '14 at 11:53
  • Edited. The error remains. – Wafa Marouani Apr 22 '14 at 13:48
  • i think ur facing the problem with cross domain request.see this link.may be it will solve ur problem related to cross domain request.link is here "http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource-w – Abhijit Mali Apr 23 '14 at 05:26
  • I solved the cross-domain problem with adding the `jsonp`attribute to `$.post();` – Wafa Marouani Apr 23 '14 at 09:20
  • is it working correctly? – Abhijit Mali Apr 23 '14 at 09:36
  • The error is not thrown anymore. Another error has been shown, it coudn't find the method addTest until I put : `public function addTest_get() { $desc = $this->get('description'); $this->response(array("description" => $desc), 200); }`. I still don't know why. But that's the solution. – Wafa Marouani Apr 23 '14 at 10:21

2 Answers2

0

You need to do the following changes, check your console for errors too:

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
Nil'z
  • 7,487
  • 1
  • 18
  • 28
  • Still not working. When I use chrome's debugger, I get this policy error, although I could access it past times : XMLHttpRequest cannot load http://mysite-url/webservices/mycontroller/addTest. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. – Wafa Marouani Apr 22 '14 at 13:34
0

Please check config.xml file on :root-android-project/res/xml add this line

Sample Code

<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" />

OR

About the Access-Control-Allow-Origin problem, I faced the same error and solved by placing this line <?php header('Access-Control-Allow-Origin: *'); ?> in the index.php which is in the root of the project.