我创建了一个返回此字符串的python脚本:
" {\" ping_response \":\" 8.8.8.8 \"}"
代码:
function firstping(){
$.ajax({
type: "GET",
url: "http://myserver:8000/pingnew",
dataType: "json",
success: function (result) {
ping_response = JSON.parse(result).resp_body;
},
error: function (msg) {
// alert(msg.responseText);
}
})
};
但是,当我使用浏览器进行调试时,我总是会收到此错误:
syntax error json.parse unexpected end of data at line 1 column 1 of the json data
当我通过tcpdump捕获流量时,我可以看到GET用字符串回答,就像我之前粘贴的一样。
这是python:
"什么是resp_body?"
这是python脚本:
def printinfo():
"This prints a passed info into this function"
resp_body = {}
resp_body['ping_response']="8.8.8.8"
return json.dumps(resp_body);
来自浏览器的错误消息:
https://i.stack.imgur.com/ujTHx.png
我在之前看不到的浏览器中有另一条错误消息:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://myserver:8000/pingnew. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
"问题已解决"
问题出在python脚本中:
@ hug.get(' / pingnew',output = hug.output_format.json,response_headers = [])
没有json.dumps的退货
删除返回后的跨源请求阻止添加
@ hug.response_middleware() def process_data(请求,响应,资源): response.set_header(' Access-Control-Allow-Origin',' *')