【发布时间】:2015-07-12 13:00:06
【问题描述】:
我正在使用超薄微框架开发一个 php 应用程序
那是我的 index.php 文件:
<?php
require 'Slim/Slim.php';
include 'db.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get("/", function () {
echo "<h1>HELLO USER</h1>";
});
//Registration view
$app->post("/reg", function (){
$request = $this->app->request();
$username = $request->post('username');
$password = $request->post('password');
$name = $request->post('name');
$email = $request->post('email');
try {
$sql = "INSERT INTO users (username, hash, name, email) VALUES (:username, :password, :name, :email)";
$s = $this->dbh->prepare($sql);
$s->bindParam("username", $username);
$s->bindParam("hash", $password);
$s->bindParam("name", $name);
$s->bindParam("email", $email);
$s->execute();
} catch(\PDOException $e) {
echo 'Exception: ' . $e->getMessage();
}
}); // Login Function End
$app->get('/updates', function () {
//Display users
});
// run the Slim app
$app->run();
?>
我有 index.html 文件,其中包含像这样的 ajax 跨域请求:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>INDEX</title>
<link href='css/style.css' rel='stylesheet' type='text/css'/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#put").submit(function(event) {
event.preventDefault();
$.ajax({
url: 'http://localhost/slim/reg',
type: 'POST',
crossDomain: true,
data: $("#put").serializeArray(),
success: function(data) {console.log(data); }
});
});
});
</script>
</head>
<body>
<form id="put">
Registration <br/>
username: <input type="text" name="username" id="username"/><br />
password: <input type="password" name="hash" /><br />
name: <input type="text" name="name" /><br />
email: <input type="text" name="email" /><br />
<input type="submit" />
</form>
</body>
</html>
问题是当我尝试在我的 index.html 文件中提交表单时,我总是得到:
致命错误:在第 19 行的 C:\wamp\www\slim\index.php 的对象上下文中使用 $this
【问题讨论】:
-
你试过删除
$this->? -
我将第 19 行设为这样: $request = $app->request();我在控制台中遇到错误:500 Internal server error
标签: php ajax cross-domain slim