2

Good morning community,

currently Im writing a plugin (account managing for a dev-tool called "Construct 2" (scirra.com)) in Javascript which uses a PHP backend and communicates with it via AJAX.

When the backend script only contains simple tasks, like...

<?php header("Access-Control-Allow-Origin:*"); // Allow all origins

$InputAction = $_POST["Action"]; // Eventhandler

if ($InputAction == "Register") {
      echo("-400") } ?>

everything works fine. But when I put in the bit more complex stuff I wanted to include, Im always getting a CORS denial:

XMLHttpRequest cannot load http://api.proxy.wtf/debug.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.0.14:50001' is therefore not allowed access. The response had HTTP status code 500.

The code I used for the above error:

<?php
header("Access-Control-Allow-Origin:*"); // Allow all origins

require('includes/config.php'); // Prerequisite

/** Numeric callbacks (!negative values!)
-200 Registration successful; validation mail sended
-250 Username OK

-300 Username too short
-301 Username already in use
-302 Password too short
-303 Invalid email address
-304 Email already in use
-305 Error while registration

-400 Illegal request
**/

$InputAction = $_POST["Action"]; // Eventhandler

$InputUsername = $_POST["Username"]; // Requesting username
$InputMailaddress = $_POST["Mailaddress"]; // Requesting mail address
$InputPassword = $_POST["Password"]; // Requesting password

if ($InputAction == "Register") { // Action: Register
   if(strlen($InputUsername) < 3){ // Check Username length
      $error[] = 'Username is too short.';
      echo("-300");
   } else { // Check if username already exists
      $stmt = $db->prepare('SELECT username FROM members WHERE username = :username');
      $stmt->execute(array(':username' => $InputUsername));
      $row = $stmt->fetch(PDO::FETCH_ASSOC);
      echo("-250");

      if(!empty($row['username'])){ // If username already taken
         $error[] = 'Username provided is already in use.';
         echo("-301");
      }
   }
}
else {
   echo("-400");
}
?>

Has someone an idea for me, what Im doing wrong? The syntax doesnt contain errors (as far as I can see). Unless Im no expert in php/ajax, I think that some minds here could help me out/point me where my mistake is at. Im willing to learn - so if Im doing something like a common mistake, please tell me :s

Have a great day, Tan

Edit: Heres the JS part http://pastebin.com/iABkRmt0 (reuqesting stuff starts at line ~115 - its the complete JS SDK script for C2, sorry for that - but at least its complete ;))

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197

2 Answers2

-1

You have to use jsonp,

I am giving you an example of Jsonp usage, hope this will help u

$.ajax({
  url: "http://data.acgov.org/resource/k9se-aps6.json?city=Alameda",
  jsonp: "$jsonp",
  dataType: "jsonp"
}).done(function(data) {
  console.log("Request received: " + data);
});
Amit Agarwal
  • 396
  • 3
  • 12
-1

you have not mentioned about ajax code.. is that ajax request also initiated from same url or it is seperate...?

BEJGAM SHIVA PRASAD
  • 2,181
  • 1
  • 18
  • 27