douxiongzhen2126 2013-07-26 05:51
浏览 143
已采纳

使用Tastypie api的jquery get方法的401 UNAUTHORIZED错误

As of now I am not using authentication for Tastypie but I am able to see content, when I go to url in browser.

http://localhost:8000/live/api/update/?format=json

but I am trying to get this data in the page via an jquery ajax call,

$.post('/live/api/update/?format=json',
            {type:'GET',dataType: "json", processData:  false,
  contentType: "application/json",userid:$('#index').val()},function(devicelist){       
    .....       
    }

In browser firebug console, I m seeing a 401

Note : from Haris's Answer, I was able to solve the issue, but I want why it works

when I use

$.ajax({ type: "POST", url: url, data: data, success: success, dataType: dataType });

it is working(Status:202) whereas when I use

  $.post('/live/api/update/?format=json',
                {type:'GET',dataType: "json", processData:  false,
      contentType: "application/json",userid:$('#index').val()},function(devicelist){       
        .....       
        }

this is not working.Actually I shifted my PHP code to Django, when I used PHP the above code used to work with 401 error

There is no Authentication in tastypie api code

api.py

from tastypie.resources import ModelResource
from models import Update
from tastypie.serializers import Serializer
import urlparse

class urlencodeSerializer(Serializer):
    formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode']
    ....


class UpdateResource(ModelResource):
    class Meta : 

        queryset = Update.objects.all()
        resource_name = 'update'
        filtering = {'imei' : ALL }
        #authentication = DjangoCookieBasicAuthentication()
        serializer = urlencodeSerializer() # IMPORTANT 
        allowed_methods = ['get','post']
  • 写回答

1条回答 默认 最新

  • doujuanxun7167 2013-07-26 08:53
    关注

    you're sending a $.POST request with jquery, yet you're trying to change the type to a GET. Use .ajax if you want to add custom options to the ajax request.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题