我是API和AJAX的新手,在这里我遇到了一个问题。我已经在PHP (Symfony)中创建了一个端点。
当我用Postman在这个端点上调用GET时,我得到了正确的答案。

但是当我尝试用AJAX得到答案时,我得到了一个500错误。下面是我的代码:
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<button id="testButton">TEST</button>
<script>
$('#testButton').click(function (e) {
let week = 42;
let year = 2021;
let data = JSON.stringify({
id:1,
week: week,
year: year}
);
$.ajax({
url: '{{ path('bookings')}}',
type: 'GET',
contentType: "application/json",
dataType: 'json',
data: data,
success: function (data, status) {
console.log(status)
if (status === 'success') {
console.log(data);
console.log('success');
} else {
console.log('failure');
}
}
});
});
</script>Path(bookings)返回127.0.0.1:8000/bookings。
下面是我得到的错误:

您能告诉我哪里弄错了吗?
发布于 2021-10-10 14:35:18
使用jQuery发送GET请求时,可以将data属性作为PlainObject传递
在不使用stringify的情况下尝试:
let data = {
id:1,
week: week,
year: year
};https://stackoverflow.com/questions/69516011
复制相似问题