Django序列化json可以在控制台中工作,但不能在带有ajax请求的页面中工作

发布于 2024-11-26 02:14:52 字数 1662 浏览 0 评论 0原文

我已经查看了与此相关的其他问题,但尚未找到可以回答我的(第一!)问题的内容:

我正在序列化 django 视图并使用 ajax (jquery)将其发送到服务器。我收到序列化数据(使用警报进行测试并收到 JSON 数据,200 ok 响应),但无法成功执行以下操作:

$('div#nextSet' + currentLetter).click(function(){
      var output = '';
            $.ajax({
                url: 'path/to/django/view',
                data: {'data':sentToServer},
                datatype: 'json',
                error: function(xhr_data) {
                    display_error();
                },
                success: function(data) {
                    $.each(data, function(i){
                        var firstName =data[i].fields.first_name;
                        var lastName = data[i].fields.last_name;
                        var portrait = data[i].fields.portrait;
                        var output = '<ul><li>' + firstName + ' ' + lastName + '</li>';
                        output += '<li><img src="' + portrait + '" /></li></ul>';
                        alert(output);
                    });
                }
            });
        });

此代码适用于控制台中的 google chrome 和 firefox(警报显示示例 html) ),但无法在页面上运行。我收到以下错误:
未捕获的类型错误:无法读取未定义的属性“first_name”。

我已经尝试使用控制台来查看如何访问 javascript 地图/字典项目,并且在输入以下内容时能够获取一个值,并将 i 替换为实际数字: 数据[数字].fields.first_name 该值显示在控制台中,并且不会出现错误消息。

这是从 django 视图返回的单个项目的数据示例: var data = [{"pk": 8, "model": "app.model", "fields": {"portrait":
“this/is/the/photo/path.png”,“first_name”:“名字”,“last_name”:“姓氏”}},]。

所有这些都嵌入在成功代码中,这意味着除非回调可用,否则不应执行任何操作(?)。因此,如果数据存在 - 当警报(数据)正在工作并且我从服务器收到 200 ok 响应 - 并且代码正在控制台中工作 - 意味着警报已处理并出现,是否有我忽略的东西?

I've looked at other questions related to this one, but have yet to find something that answers my (first!) question:

I'm serializing a django view and sending it to the server with ajax (jquery). I receive the serialized data (tested it with alert and received the JSON data, 200 ok reponse), but am unable to get the following to work on success:

$('div#nextSet' + currentLetter).click(function(){
      var output = '';
            $.ajax({
                url: 'path/to/django/view',
                data: {'data':sentToServer},
                datatype: 'json',
                error: function(xhr_data) {
                    display_error();
                },
                success: function(data) {
                    $.each(data, function(i){
                        var firstName =data[i].fields.first_name;
                        var lastName = data[i].fields.last_name;
                        var portrait = data[i].fields.portrait;
                        var output = '<ul><li>' + firstName + ' ' + lastName + '</li>';
                        output += '<li><img src="' + portrait + '" /></li></ul>';
                        alert(output);
                    });
                }
            });
        });

This code works in google chrome and firefox in the console (alert shows with sample html), but fails to run on the page. I'm getting the following error:
Uncaught TypeError: Cannot read property 'first_name' of undefined.

I've tried the console to see how to access javascript map/dictionary items, and am able to get a value when entering the following, replacing i with an actual number:
data[number].fields.first_name
The value is shown in the console and no error message appears.

This is a sample of the data returned from the django view for a single item:
var data = [{"pk": 8, "model": "app.model", "fields": {"portrait":
"this/is/the/photo/path.png", "first_name": "First", "last_name": "LastName"}}, ].

All of this is embedded in the success code, meaning nothing should be executed(?) unless the callback is available. So if data is present -- as alert(data) is working and I get a 200 ok response from the server -- and the code is working in console -- meaning the alert is processed and appears, is there something I'm overlooking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小瓶盖 2024-12-03 02:14:52

事实上,您收到“未捕获的类型错误:无法读取未定义的属性‘first_name’”。表明该字段未在数据上定义,但您实际上获取了数组中的对象。

一种想法可能是打印数组对象的控制台语句。

只需添加:

console.log(data[i])

直接在每个函数内部

原因可能与 this 有关

The fact that you get "Uncaught TypeError: Cannot read property 'first_name' of undefined." suggests that field is not defined on the data, but that you actually get the object in the array.

One idea could be to print a console statment of the array object.

Just add:

console.log(data[i])

directly inside the each function

The reason might be related to this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文