я пытаюсь сделать запрос API, используя ajax на laravel, но он продолжает выдавать мне ошибку кода 500, я уже тестирую URL-ссылку, сгенерированную из URL-адреса ajax в браузере, и она работает нормально, но когда она использует функцию ajax, она сохранит код возврата 500.
это мой js
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name = "csrf-token"]').attr('content')
}
});
document.getElementById("infoEmpty").style.display = 'none';
document.getElementById("infoError").style.display = 'none';
$.ajax({
url: "/api/prospec-survey",
data: {"change_code":"A", "change_state_code":2, "change_active_state":1},
method: 'GET',
dataType: "text json",
type: 'GET',
success: function(data){
$("table tbody").html('')
data.forEach(function(item, index){
$("table tbody").append(
`<tr>
<td style = "border-right: 1px solid gray; text-align: center;">${item.survey_nomor}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.survey_date}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.contract_id}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.contract_nama}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.class_code}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.user_name}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.route_desc}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.locatioin_komplek}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.pp_name}</td>
<td style = "text-align: center;"><a href = "/customer-care/new-connection/survey-new-customers/print-survey-order/{${item.survey_nomor}}" class = "badge bg-primary"><i class = "bi bi-printer-fill"></i></a></td>
</tr>`
)
})
document.getElementById("table").style.display = 'block';
},
error:function(rr){
document.getElementById("infoError").style.display = 'block';
document.getElementById("infoEmpty").style.display = 'none';
document.getElementById("table").style.display = 'none';
}
});
и маршруты API
Route::apiResource('prospec-survey', ProspectiveCustomersSurveyController::class);
и это дает мне ошибку, как это
jquery.min.js:4 GET http://127.0.0.1:8001/api/prospec-survey?change_code=A&change_state_code=2&change_active_state=1 500 (Internal Server Error)
но если я использую URL-адрес из ajax в браузере напрямую, он возвращает данные, как и ожидалось, как показано ниже.
Illuminate\Pagination\LengthAwarePaginator {#1823 ▼
#items: Illuminate\Support\Collection {#1805 ▶}
#perPage: 10
#currentPage: 1
#path: "http://127.0.0.1:8001/api/prospec-survey"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
#total: 54
#lastPage: 6
}
есть идеи, как это исправить? любое предложение может действительно помочь






Хорошо, после некоторого быстрого обзора я обнаружил, что ошибка исходит от самого контроллера API, из-за чего ответ не возвращается как json, а умирает, и блокирует запрос ajax функцией dieump.
$data = $this->Survey($request, $select_data, $custom_where);
dd($data) // -> this blocked the response
return response()->json($data);
и еще одна ошибка связана с циклом ajax, где сами данные были разбиты на страницы, где циклы не могут получить доступ к фактическим данным, поэтому я меняю jquery с этого
data.forEach(function(item, index){ // <- cannot access the paginate data
$("table tbody").append(
`<tr>
<td style = "border-right: 1px solid gray; text-align: center;">${item.survey_nomor}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.survey_date}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.contract_id}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.contract_nama}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.class_code}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.user_name}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.route_desc}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.locatioin_komplek}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.pp_name}</td>
<td style = "text-align: center;"><a href = "/customer-care/new-connection/survey-new-customers/print-survey-order/{${item.survey_nomor}}" class = "badge bg-primary"><i class = "bi bi-printer-fill"></i></a></td>
</tr>`
)
})
хотя это
data.data.forEach(function(item, index){ // <- adding 1 level data so it can access the paginate value
$("table tbody").append(
`<tr>
<td style = "border-right: 1px solid gray; text-align: center;">${item.survey_nomor}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.survey_date}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.contract_id}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.contract_nama}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.class_code}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.user_name}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.route_desc}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.locatioin_komplek}</td>
<td style = "border-right: 1px solid gray; text-align: center;">${item.pp_name}</td>
<td style = "text-align: center;"><a href = "/customer-care/new-connection/survey-new-customers/print-survey-order/{${item.survey_nomor}}" class = "badge bg-primary"><i class = "bi bi-printer-fill"></i></a></td>
</tr>`
)
})
Необходимо опубликовать вкладку «Журналы и ошибки из сети» в инструментах разработчика.