Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax POST keep firing error function event though php script success
    text
    copied!<p>I am using jquery, jquery mobile and html to build a mobile app using phonegap.</p> <p>Using ajax to call php scripts in server, works fine; updating, inserting data and sending email. However ajax error function is what always called, Using firebug, 200 ok status is what returned, no idea what triggers error. </p> <p>I have searched for this issues and found many suggestions here such as using complete () or done() functions instead of success() and error(), echoing json response... but no luck with all these solutions. </p> <p>one solution is to add this header to php script,</p> <pre><code>header("Access-Control-Allow-Origin: *"); </code></pre> <p>header solution has solved my problem with all ajax post scripts, however I am concerning about possible security risks because it is should run in mobile application. </p> <p>In addition, adding such header can be logic when we need to access database but if we have a contact form that call php mail function and no database changes will occur , why we need to add that header too? </p> <p>contact form </p> <pre><code>&lt;div data-role="header" data-position="fixed" &gt; &lt;input type="button" value="Send" name="sendbutton" id="sendbtn" /&gt; &lt;h1&gt;contact us&lt;/h1&gt; &lt;a data-rel="back" data-icon="arrow-r" data-iconpos="notext"&gt;&lt;/a&gt; &lt;/div&gt; &lt;div data-role="content" &gt; &lt;form id="contact-us" &gt; &lt;input type="text" id="contactName" placeholder="Name" /&gt; &lt;input type="email" id="email" placeholder="Your Email" /&gt; &lt;input type="text" id="subject" placeholder="Message Subject" /&gt; &lt;textarea rows="20" cols="30" id="message" placeholder="Message.." &gt;&lt;/textarea&gt; &lt;/form&gt; &lt;/div&gt; </code></pre> <p>Jquery script</p> <pre><code> $("#sendbtn").click(function(event){ $("#sendbtn").disabled =true; var postData =""; if($("#contactName").val().length == 0 ||$("#email").val().length == 0 ||$("#subject").val().length == 0||$("#message").val().length == 0) alert("Sorry but some required fields are empty !"); else { if (!regex.test($("#email").val())) alert("Please check that you write email correctly!"); else { postData='contactName=' + $("#contactName").val() + '&amp;email=' + $("#email").val() + '&amp;subject=' + $("#subject").val() + '&amp;message=' + $("#message").val(); $.ajax({ type: 'POST', data: postData, url: 'mydomainname.net/services/contact.php', success: function(){ $('input').not('[type="button"]').val(''); $('textarea').val(''); alert('Message was Sent, Thank you!'); }, error: function () { alert('Sorry, Error occurred '); $('.error').fadeIn(1000); } }); } } }); </code></pre> <p>php code </p> <pre><code>&lt;?php if ( isset($_POST['email']) &amp;&amp; isset($_POST['contactName']) &amp;&amp; isset($_POST['message']) &amp;&amp; filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) { $test = "/(content-type|bcc:|cc:|to:)/i"; foreach ( $_POST as $key =&gt; $val ) { if ( preg_match( $test, $val ) ) { exit; } } mail( "myemail@mydomainname.net", "Contact Form: ".$_POST['contactName']." Title ".$_POST['subject'], $_POST['message'], "From:" . $_POST['email']); } ?&gt; </code></pre> <p>The code send email but always called ajax error function</p> <p>Any other solution rather than header solution? thank you. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload