Python 字符串为 null 但也不是 None 也不是空字符串
Python string is null but also isn't None nor empty string
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
我应该如何检查它是 None?为什么它说它是 null 而不是 None?
我正在使用 AppEngine。
我的 javascript 发出 POST 请求的代码:
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) {
...
}
});
我改为对 POST 请求使用 application/json
而不是默认的 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({
url: url,
type: 'POST',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({'playlist': url_id,'language': url_language}),
success: function(results) {
...
},
error: function(response, status, error) {
...
}
});
并且后端接收它是这样的:
class StartAnalysis(BaseHandler):
def post(self):
data = json.loads(self.request.body)
playlist = data['playlist']
language = data['language']
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
我应该如何检查它是 None?为什么它说它是 null 而不是 None?
我正在使用 AppEngine。
我的 javascript 发出 POST 请求的代码:
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) {
...
}
});
我改为对 POST 请求使用 application/json
而不是默认的 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({
url: url,
type: 'POST',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({'playlist': url_id,'language': url_language}),
success: function(results) {
...
},
error: function(response, status, error) {
...
}
});
并且后端接收它是这样的:
class StartAnalysis(BaseHandler):
def post(self):
data = json.loads(self.request.body)
playlist = data['playlist']
language = data['language']