2013-07-26 108 views
0

截至目前我没有使用认证的Tastypie,但我能够看到内容,当我去浏览器中的网址。401未授权的错误与jQuery的get方法与Tastypie api

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

,但我想通过一个jQuery的AJAX调用来获取网页中的相应数据,

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

在浏览器Firebug的控制台,我米看到401

注:从哈里斯的回答: ,我能解决这个问题,但我想它为什么有效

当我使用

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

这是工作(状态:202),而当我使用

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

这不是working.Actually我转移我的PHP代码,Django的,当我用PHP使用上面的代码与401错误工作

有一个在tastypie API代码

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

你没什么认证用jQuery结束一个$。POST请求,但是你正在尝试将类型改为GET。如果要将自定义选项添加到ajax请求,请使用.ajax。