我試圖在 laravel 上使用 ajax 發出 api 請求,但它一直給我代碼 500 錯誤我已經在瀏覽器上測驗了從 ajax url 生成的 url 鏈接,它作業正常,但是當它使用 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(內部服務器錯誤)
但是如果我直接在瀏覽器上使用來自 ajax 的 url,它會按預期回傳資料,如下所示
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
}
知道如何解決嗎?任何建議都可能真的有幫助
uj5u.com熱心網友回復:
好的,經過一些快速審查后,我發現錯誤來自 api 控制器本身,它使回應不以 json 形式回傳,而是以 dieump 的形式回傳,并使 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>`
)
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/454966.html
上一篇:連接Ajax序列化資料
