日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

使用rest_framework写api接口的一些注意事项(axios发送ajax请求)

發布時間:2023/11/27 生活经验 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用rest_framework写api接口的一些注意事项(axios发送ajax请求) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 類繼承GenericAPIView,定義queryset

印象深刻的事:
由于原來對于繼承關系不太清楚,寫接口 APIView/泛指GenericAPIView不太關注queryset
沒有設置渲染器:默認 [JSONRenderer,BrowsableAPIRenderer]
BrowsableAPIRenderer,內部檢查當前視圖函數是否有 get_queryset,如有則會調用。未設置,則斷言異常。

2. 所有視圖都有功能:添加到配置文件

比如統一渲染器,解析器等

3. URL統一規則

url后加不加/等,都要統一

4. 序列化?

- 簡單:fk/o2o/choice -> source
- 復雜:m2m/gfk -> SerializerMethodField

在序列化類中定義字段時,對于一些簡單的外鍵、一對一字段或choice字段可以使用source方法獲取想要的值

而復雜一些的多對多字段等可以使用自定義方法

class CourseSerializer(ModelSerializer):category = serializers.CharField(source='sub_category.name')xx = serializers.CharField(source='get_course_type_display')price_policy = serializers.SerializerMethodField()class Meta:model = models.Coursefields = ['id', 'name','category','xx','price_policy']def get_price_policy(self, obj):price_policy_list = obj.degreecourse_price_policy.all()return [{'id': row.id, 'price': row.price, 'period': row.get_valid_period_display()} for row inprice_policy_list]

5. cors

跨域請求可以通過中間件添加相應的響應頭,一些參數還可以寫到settings中

class Cors(MiddlewareMixin):def process_response(self, request, response):response['Access-Control-Allow-Origin'] = ','.join(settings.CORS_ORIGIN_LIST)if request.method == 'OPTIONS':response['Access-Control-Allow-Methods'] =  ','.join(settings.CORS_METHOD_LIST)response['Access-Control-Allow-Headers'] = ','.join(settings.CORS_HEADER_LIST)response['Access-Control-Allow-Credentials'] = 'true'return response

使用axios發送ajax請求

首先要下載axios

npm install axios --save

然后在main.js中導入,并使用Vue.prototype.$axios = axios方法,讓我們可以在后面使用this.$axios使用它

?

import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/store'
import axios from 'axios'Vue.prototype.$store = store
Vue.prototype.$axios = axios
axios.defaults.headers['Content-Type'] = "application/json"Vue.config.productionTip = false

這里axios.defaults.headers['Content-Type'] = "application/json"的意思是后續的axios發送請求時請求頭中都會帶有Content-Type='application/json',ajax中也能做相應的設置,如下

$.ajaxSetup({beforeSend: function(xhr, settings) {xhr.setRequestHeader("Content-Type", "application/json");}
});

使用

init(){// 發送Ajaxvar that = thisthis.$axios.request({url: this.$store.state.apiList.course,method:'GET',params:{course_type:1}}).then(function (arg) {console.log('then',arg)that.courseList =arg.data.data}).catch(function (arg) {console.log('catch',arg.response)})
}

通過axios.request方法發起ajax請求,參數url是發送的地址,method是發送方法,params是url中的參數,如果要發送請求體中的參數則使用data

then相當于ajax中的success,catch相當于error

轉載于:https://www.cnblogs.com/weiwu1578/articles/8909014.html

總結

以上是生活随笔為你收集整理的使用rest_framework写api接口的一些注意事项(axios发送ajax请求)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。