Php AJAX返回404
我试图将JSON从Twig模板发送到PHP类,该类将保存在it数据库中,但我发现404错误 这是我的小枝模板的内容。我的目标是发送已注释的Php AJAX返回404,php,ajax,symfony,Php,Ajax,Symfony,我试图将JSON从Twig模板发送到PHP类,该类将保存在it数据库中,但我发现404错误 这是我的小枝模板的内容。我的目标是发送已注释的datavar,但出于测试目的,我替换了它 function saveUser() { var e = document.getElementById("subject_select"); var subject = e.options[e.selectedIndex].text; console.log(subject);
data
var,但出于测试目的,我替换了它
function saveUser()
{
var e = document.getElementById("subject_select");
var subject = e.options[e.selectedIndex].text;
console.log(subject);
var e2 = document.getElementById("mark_select");
var mark = e2.options[e2.selectedIndex].value;
console.log(mark);
var e3 = document.getElementById("student_select");
var student = e3.options[e3.selectedIndex].value;
console.log(student);
//var data = ({ subject:subject, mark:mark, student:student}).serialize();
$.ajax({
type: "POST",
url: 'save/',
data: {subject:"a", mark:1, student:2},
success: function(response)
{
console.log(response);
}
});
php中的和类:
class TestMark extends Controller
{
/**
* @Route("/save/", name="_save")
*/
public function saveAction(){
$mysqli = new mysqli("localhost", "root", null, "school");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$this->getRequest()->isXmlHttpRequest())
{//no ajax request, no play...
$this->redirect(
$this->generateUrl('_gradebook')
);
}
$jsonString = $this->getRequest();
$jsonArray = json_decode($jsonString, true);
/* create a prepared statement */
if ($stmt = $mysqli->prepare('INSERT INTO marks (studentId, mark, subject) VALUES (?,?,?)')) {
/* bind parameters for markers */
$stmt->bind_param("ssss", $jsonArray['subject'], $jsonArray['mark'], $jsonArray['student']);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
}
}
更新:
经过一些更改后,我从Chrome控制台收到错误500的日志:
GET http://host/save/ 500 (Internal Server Error)
l.cors.b.crossDomain.send @ jquery.min.js:4
n.extend.ajax @ jquery.min.js:4
n.(anonymous function) @ jquery.min.js:4
saveUser @ gradebook:66
onclick @ gradebook:125
JS和ajax:
<input type="hidden" value="{{ path('_save') }}" id="save_url">
<script>
function saveUser()
{
var requestUrl = $('#save_url').val();
var e = document.getElementById("subject_select");
var subject = e.options[e.selectedIndex].text;
console.log(subject);
var e2 = document.getElementById("mark_select");
var mark = e2.options[e2.selectedIndex].value;
console.log(mark);
var e3 = document.getElementById("student_select");
var student = e3.options[e3.selectedIndex].value;
//var data = ({ subject:subject, mark:mark, student:student }).serialize();
console.log(student);
$.post('{{path('AppBundle_ajax_update')}}',
{subject: 'mydata1', mark:'mydata2', student:'mydata3'},
function(response){
if(response.code == 100 && response.success){//dummy check
//do something
}
}, "json");
}
注意:重命名您的控制器类
TestMark
至
TestMarkController
您的控制器没有返回任何响应。尝试返回:
return new Symfony\Component\HttpFoundation\Response('student saved');
在您关闭mysql连接之后
我还建议在细枝模板上获取请求url。像这样:
<input type="hidden" value="{{ path('_save') }}" id="save_url">
正如Imanali Mamadiev所建议的,您应该重命名控制器类。注意:您的bind语句中有4个参数(
sss
),但您只提供3个值,并且在查询中只有3个起搏器。如果将url:'save/',
更改为url:'/save/',会发生什么情况,
在您的ajax调用中?您应该阅读。没有此占位符,使用/save/而不是save/,仍然没有任何内容。您可以通过运行php bin/console debug:router来获取symfony2应用程序中的所有URL
是的,我取得了进展。现在它返回500个内部服务器错误l.cors.b.crossDomain.send@jquery.min.js:4 n.extend.ajax@jquery.min.js:4 saveUser@gradebook:67 onclick@gradebook:114
您是否在调试模式下请求?像/app\u dev.php//\u save?请求url是什么?请尝试app\u dev.php/save/
,您应该会得到一个错误。我更正了一些错误,现在查询中只有一个是由null引起的。但它仍然不起作用。再次更新类代码
<input type="hidden" value="{{ path('_save') }}" id="save_url">
var requestUrl = $('#save_url').val();
$.ajax({
...
url: requestUrl,
...
});