title: Get json ajax php form to work remotely
body: <p>So, essentially I have this Javascript which interprets the JSON from my php code.  It works great on the local server, however, when I try to move the script to a different server it will not work.  I have added the </p>
  
  <pre><code>&lt;?php header("Access-Control-Allow-Origin: *"); ?&gt;
  </code></pre>
  
  <p>also i've declared my database connection as global within my PHP function.</p>
  
  <p>I'm confused as to why these solutions aren't working..  Also, I understand some of the script is iffy, but I'm only interested in figuring out why its not working from different servers.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $("document").ready(function(){
    $(".sendText").submit(function(){
      $("#sendButton").prop("disabled",true);
      $(".errors").html("");
      $(".success").html("");
      var data = {
        "action": "test"
      };
      data = $(this).serialize() + "&amp;" + $.param(data);
      $.ajax({
        type: "POST",
        dataType: "jsonp",  //new edit
        url: "http://myurl.com/testing/jsonpost.php?callback=test",  //new edit
        data: data,
        success: function(data) {
          if(data["success"]=="yes") {
          $(".success").html("Message Sent!");
          $(".formContainer").html("" + data["json"] + "");
          }
          else {
          if(document.getElementById("sendButton").disabled = true){ document.getElementById("sendButton").disabled = false; }
          $(".errors").html("" + data["errors"] + "");
          }
        }
      });
      return false;
    });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>Some Info when I look at the web console from firebug:</p>
  
  <pre><code>Access-Control-Allow-Orig...    *
  Connection  Keep-Alive
  Content-Length  0
  Content-Type    application/json
  Date    Wed, 24 Sep 2014 04:22:57 GMT
  Keep-Alive  timeout=5, max=100
  Server  Apache/2.2.27 (Unix) mod_ssl/2.2.27 OpenSSL/1.0.1e-fips DAV/2 mod_bwlimited/1.4
  X-Powered-By    PHP/5.4.29
  </code></pre>
  
  <p>Looks like it is communicating with server but not able to interpret data? thoughts?</p>
  
  <p>Also, this error comes up in the console from the remote server but not when I run on local server:</p>
  
  <pre><code>SyntaxError {stack: (...), message: "Unexpected end of input"}message: "Unexpected end of input"stack: (...)
  Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…
  parsererror 
  </code></pre>
  
  <p>The PHP code is pretty long (and I prefer not to release all of it) - however here is the shortened version:</p>
  
  <pre><code>&lt;?php
  header("Access-Control-Allow-Origin: *");
  header('Content-Type: application/json');
  require "../database/db.php";
  
  if (is_ajax()) {
    if (isset($_POST["action"]) &amp;&amp; !empty($_POST["action"])) { //Checks if action value exists
      $action = $_POST["action"];
      switch($action) { //Switch case for value of action
        case "test": test_function(); break;
      }
    }
  }
  
  //Function to check if the request is an AJAX request
  function is_ajax() {
    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
  }
  
  function test_function(){
  $c="1";
  global $con;
  
    $return = $_POST; //to reference post
  
  $content=$return["content"];
  
  //see if content is 140 characters or less
  if(strlen($content)&gt;140){ $c="0"; $lerror="&lt;li&gt;Your message must be 140 characters or less in length!&lt;/li&gt;"; }
  
  
  
  if($c=="0"){  //doesnt pass validation
  $return["success"]="no";
  $return["errors"]="$lerror";
  }
  
  if($c!="0"){ //passes validation
  $return["success"]="yes";
  }
  if(isset($_GET['callback'])){  //jsonp edit
  $return["json"] = json_encode($return);
  echo $_GET['callback']."(".json_encode($return).")"; //jsonp edit
  }
  
  }
  </code></pre>
  
  <p>Also after converting to JSONP on remote server - get error -</p>
  
  <pre><code>"jQuery111006159528985153884_1411663761720 was not called"
  </code></pre>