Django 和 Ajax:语法错误:JSON 中在解析 () 处位置 0 处出现意外的标记 D<anonymous>



我的项目的后端是django(我正在处理项目的第4页(。 我有一系列表单和输入,它们接受输入数据,然后将输入滚动到 json 对象中。 在使用 stringify (var modelData_toserver = JSON.stringify(modelData( 后,我已经检查以确保 json 对象有效(从控制台中提取它并使用了 json 验证器(。 生成的 json 为:

{"data":[{"depth":[0]},{"temp":[1]},{"lakename":"m"},{"lakeId":"m"},{"area":"1"},{"fetch":"1"},{"maxDepth":"1"},{"lat":"1"},{"long":"1"},{"airTemp":"1"},{"shelterFactor":"1"},{"extinctCoeff":"1"},{"dispersion":"1"},{"longWave":"1"}]}

我的 ajax 代码是(modelData_toserver是 json(:

function sendInputs(modelData_toserver, name){
var urlViews = "/beach/bmominputs/" + name + "/"
console.log(urlViews)
$.ajax({
url: urlViews,
type:"POST",
dataType: "json",
data: modelData_toserver,
error: function(jqXHR, textStatus, errorThrown) {
alert('Lake Characteristics Data Not Delivered Properly');
$('#result').html('<p>status code: '+jqXHR.status+'</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
console.log('datatype:');
console.log(typeof data);
},
success: function(data, textStatus, jqXHR) {
alert('Data Sent Sucessfully');
}
});

我很确定我的 url.py 正确地指向我的视图,因为如果我直接在浏览器中输入我的 URL,它会打印出视图的 HttpResponse - HttpResponse("数据发布成功"(。 我怀疑 stingify javascript 引起了问题,但不确定如何以另一种方式将变量值转换为字符串。

感谢您在解决此错误时可以提供的任何帮助:语法错误:JSON 中在解析 (( 处位置 0 处出现意外的标记 D。

我想通了(更改如下(:

changed the data format that I send (got rid of quotes and the data key, cleaned it up to be ..)
var modelData = {depth: depthArray, temp: temperatureArray, lakename: lakeName.val(),lakeId: lakeId.val(), area: area.val(),fetch: fetch.val(), maxDepth: maxDepth.val(),lat: lat.val(),long: long.val(),airTemp: airTemp.val(),shelterFactor: shelterFactor.val(),extinctCoeff: extinctCoeff.val(),dispersion: dispersion.val(),longWave: longWave.val()};
``
Second, modified the content type to:

内容类型: 'application/json',

Added a whole bunch of cross-scripting stuff that I barely understand from here:
https://andrewstestingfiles.readthedocs.io/en/latest/ref/csrf.html
To get the entire json, I used the following in the view

modeldict = json.loads(request.body.decode("utf-8"((


最新更新