2013-07-26 71 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。