我正在嘗試使用 AJAX 在 html 網站上呈現 Json 回應,但我不斷收到錯誤訊息:
Uncaught ReferenceError: data is not defined
at HTMLDocument.<anonymous
有問題的 JsonResponse:
["[53.50119612705815, -1.1270833894501477] -> [53.34474, -3.01101]", "[53.50119612705815, -1.1270833894501477] -> [53.34474, -3.01101]", "[52.04061648544843, -0.6655072691644374] -> [51.90829, -0.5127]", "[52.04061648544843, -0.6655072691644374] -> [51.90829, -0.5127]", "[52.04061648544843, -0.6655072691644374] -> [51.90829, -0.5127]", "[53.50119612705815, -1.1270833894501477] -> [53.42705, -0.94339]"]
帶有 AJAX 和 JS 的 Html 檔案:
<div class="'row">
<div id="test">
<h1> Test </h1>
</div>
</div>
{% endblock %}
{% block js %}
<script>
$(document).ready(function(){
$.ajax({
type: 'POST',
dataType: 'json',
url: '/network/dispatch_data/',
data: data,
success: function(response) {
console.log(response);
$('#test').append(response.data);
}
});
});
</script>
{% endblock %}
當我在瀏覽器中檢查元素時,錯誤指向錯誤 data: data的根源。知道我做錯了什么嗎?我可以用 json 回應完美地查看 url,但是用 ajax 顯示它是一個問題
uj5u.com熱心網友回復:
您data作為請求的主體發送,但首先您必須定義要發送到 API 的物件
<div class="row">
<div id="test">
<h1> Test </h1>
</div>
</div>
{% endblock %}
{% block js %}
<script>
const data = {
bar: [1, 2, 3],
foo: false
}
$(document).ready(function () {
$.ajax({
type: 'POST',
dataType: 'json',
url: '/network/dispatch_data/',
data: data,
success: function (response) {
console.log(response);
$('#test').append(response.data);
}
});
});
</script>
{% endblock %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/472061.html
標籤:javascript 阿贾克斯
