早上好社區,從JS到PHP;另一個問題CORS(好像是)
目前我寫一個插件(帳戶管理的稱爲DEV-工具「構建體2」(scirra.com))在Javascript它使用PHP後臺並通過AJAX與它進行通信。
當後臺腳本只包含簡單的任務,像...
<?php header("Access-Control-Allow-Origin:*"); // Allow all origins
$InputAction = $_POST["Action"]; // Eventhandler
if ($InputAction == "Register") {
echo("-400") } ?>
一切工作正常。但是,當我把在複雜一點的東西,我想包括我總是得到一個CORS否認:
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.
我用上面的錯誤代碼:
<?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");
}
?>
有別人的想法我,我做錯了什麼?語法不包含錯誤(據我所見)。除非我沒有php/ajax方面的專家,否則我認爲這裏的某些人可以幫助我/指出我的錯誤所在。進出口願意學習 - 所以,如果Im做像一個常見的錯誤,請告訴我:■
有一個偉大的日子, 譚
編輯: 繼承人的JS部分http://pastebin.com/iABkRmt0(reuqesting東西開始於行〜 115 - 它的完整的JS SDK腳本C2,遺憾的是 - 但至少它的完整;))
您是否試過'header('Access-Control-Allow-Origin:http://192.168.0.14:50001');'? – Chay22