Note that there are some explanatory texts on larger screens.

plurals
  1. POCross Domain Writing to a JSON File via Ajax/JSONP calling an external PHP file
    primarykey
    data
    text
    <p><strong>UPDATED AND SOLVED</strong></p> <p>Thanks to @Christofer Eliasson for the hint. Adding:</p> <pre><code>header("access-control-allow-origin: *"); </code></pre> <p>to my PHP file solved the issue. Perhaps not the most beautiful way of dealing with the problem but this works.</p> <p>To make it a bit better/safer:</p> <pre class="lang-php prettyprint-override"><code>$http_origin = $_SERVER['HTTP_ORIGIN']; if ($http_origin == "http://domain1.com") { header('Access-Control-Allow-Origin: *'); } </code></pre> <hr> <p>Here is another <strong>cross-domain</strong> related question. </p> <p>I have a simple HTML form on domain1.com where a user can add his/her email to a mailing list, here a simple json file, "mails.json", hosted on a second domain (domain2.com).</p> <p>When the user submits his/her email, a JS script is called whose aim is to check the email and to send the data of the form (here the user's email) to the mails.json file hosted on domain2.com via ajax GET and JSONP.</p> <p>Ajax calls a PHP script hosted on domain2.com that should get the user's email and write it to mails.json. Moreover, it should send back to domain1.com some messages regarding the success or errors, given that the user has already entered his email before.</p> <p>Currently, the email is sent and saved to mails.json but I cannot manage to get my PHP script to send back messages to domain1 regarding its execution. Thanks for your advices and feel free to check and modify the code below.</p> <p>The HTML form hosted on <strong>domain1.com</strong></p> <pre class="lang-html prettyprint-override"><code>&lt;div id="mail"&gt; &lt;form method="post" action="http://domain2.com/script.php" class="notifyme"&gt; &lt;input type="email" value="" name="email" class="email" id="email" placeholder="Type your email here" required&gt; &lt;input type="submit" value="Get notified &amp;raquo;" id="submit" name="submit"&gt; &lt;div class="clear"&gt;&lt;/div&gt; &lt;/form&gt; &lt;div class="errmail hide"&gt;&lt;span class="uremail"&gt;&lt;/span&gt; is not a valid email address. Try again :)&lt;/div&gt; &lt;div class="error hide"&gt;Ouch :( &lt;span class="uremail"&gt;&lt;/span&gt; is already registered.&lt;/div&gt; &lt;div class="success hide"&gt;Thank You :) &lt;span class="uremail"&gt;&lt;/span&gt; will be notified once we're ready.&lt;/div&gt; &lt;/div&gt; </code></pre> <p>The Javascript file hosted on <strong>domain1.com</strong></p> <pre class="lang-js prettyprint-override"><code>//jQuery Initialization $(function(){ // Form $('#submit').click(function () { //onSubmit $('.error,.errmail,.success').hide(); var email = $('input[name=email]').val(); //Email validation var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&amp;'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&amp;'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i); var valid = pattern.test(email); if (!valid &amp;&amp; email !== 'undefined') { $('.errmail').removeClass('hide').show('fast'); if (!email){$('.uremail').append('This');} else{$('.uremail').append(email);} return false; } else { //start Ajax $.ajax({ url: "http://domain2.com/script.php?json_callback=?", dataType: "jsonp text", //GET method is used type: "GET", //pass the data data: 'email=' + email, //Do not cache the page cache: false, //Cross Domain crossDomain: true, //success success: function (html) { //if list.php returned 1/true (send mail success) if (html==1) { $('.success').removeClass('hide').show('fast');$('.uremail').append(email); } else if (html == 0){ $('.error').removeClass('hide').show('fast');$('.uremail').append(email); } else { alert('Sorry, unexpected error. Please try again later.'); } } }); } //cancel the submit button default behaviours return false; }); }); </code></pre> <p><strong>UPDATED</strong></p> <p>The PHP "script.php" file hosted on <strong>domain2.com</strong> </p> <pre><code>header('content-type: application/json; charset=utf-8'); $http_origin = $_SERVER['HTTP_ORIGIN']; if ($http_origin == "http://domain1.com") { header('Access-Control-Allow-Origin: *'); } $logname = 'mails.json'; $logcontents = file_get_contents($logname); //Retrieve form data. $email = ($_GET['email']) ?$_GET['email'] : $_POST['email']; //flag to indicate which method it uses. If POST set it to 1 if ($_POST) $post=1; if(strpos($logcontents,$email) !== false) { if ($_POST) {die('You are already subscribed.');} else{ $result = 0;echo $result; } } else { $filecontents = $email.','; $fileopen = fopen($logname,'a+'); $filewrite = fwrite($fileopen, json_encode($filecontents) ); $fileclose = fclose($fileopen); if(!$fileopen or !$filewrite or !$fileclose) { if ($_POST) {die('Error occured');} else{ $result = 0;echo $result; } } else { if ($_POST) {echo 'Your email has been added.';} else{ $result = 1;echo $result; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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