谁还没个明天 2012-07-25 12:54 采纳率: 50%
浏览 82

django和ajax返回数据

I'm trying to implement a quite easy ajax call but since its my first one, something goes wrong and I can't find why. After a users choice from a dropdown list I want another field of the form to be completed after the corresponding query. Here is my code: jquery:

<script type="text/javascript">
    $(document).ready(function() {
        $('#id_veh_id1').bind('click', function () {
            var val1 =$("#id_veh_id1").val();
            $.get(""+val1+"/", function(data) {
                result=data.veh_length;
                document.getElementById('id_vlength').value=result;
            });
        });
    });
</script>

with url: url(r'^(?P<user_id>\d+)/main_Webrequests/(?P<veh_id>\d+)/$', 'auth.views.test', name='test')

and view:

def test(request, veh_id, user_id):   
    message = {"veh_length": ""}
    if request.is_ajax():
        vehicle1 = Vehicles.objects.get(id = veh_id)
        veh_length = vehicle1.vlength
        message['veh_length'] = vehicle1.vlength
    else:
        message = "yoohoo"
        json = simplejson.dumps(message)
    return HttpResponse(json, mimetype='application/json')

When I try it with veh_id instead of veh_length everything seems to work fine but maybe I didn't understand sth well with the whole thing.

  • 写回答

2条回答 默认 最新

  • weixin_33709364 2012-07-25 13:34
    关注

    i think you need to serialize your json data in django code.....check this link for further reference..

    https://docs.djangoproject.com/en/dev/topics/serialization/

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题