Python 字符串为 null 但也不是 None 也不是空字符串

class StartAnalysis(BaseHandler):

    def post(self):

        playlist = self.request.get('playlist')

        language = self.request.get('language')

playlist如果我在没有此字段的情况下发出 POST 请求,则会发生这种情况:


>>> playlist

null

>>> type(playlist)

<type 'unicode'>

>>> playlist is None

False

>>> not playlist

False

>>> playlist == ''

False

>>> playlist == u''

False

我应该如何检查它是否为无?为什么说它是 null 而不是 None?


我正在使用 AppEngine。


我发出 POST 请求的 javascript 代码:


let params = new URLSearchParams(location.search);

let url_id = params.get('id');

let url_language = params.get('language');

const url = 'http://localhost:8080/start-analysis?playlist=' + url_id + '&language=' + url_language;

$.ajax({

    url: url,

    type: 'POST',

    success: function(results) {

       ...

    },

    error: function(error) {

       ...

    }

});


largeQ
浏览 120回答 1
1回答

幕布斯7119047

我改为使用application/jsonPOST 请求而不是默认值application/x-www-form-urlencoded,这似乎解决了"null"当参数之一为空或丢失时请求发送字符串而不是空字符串的问题。let params = new URLSearchParams(location.search);let url_id = params.get('id');let url_language = params.get('language');const url = 'http://localhost:8080/start-analysis';$.ajax({&nbsp; &nbsp; url: url,&nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; contentType: 'application/json',&nbsp; &nbsp; data: JSON.stringify({'playlist': url_id,'language': url_language}),&nbsp; &nbsp; success: function(results) {&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; },&nbsp; &nbsp; error: function(response, status, error) {&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }});后端接收它的方式如下:class StartAnalysis(BaseHandler):&nbsp; &nbsp; def post(self):&nbsp; &nbsp; &nbsp; &nbsp; data = json.loads(self.request.body)&nbsp; &nbsp; &nbsp; &nbsp; playlist = data['playlist']&nbsp; &nbsp; &nbsp; &nbsp; language = data['language']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python