为什么我从 fetch 调用中得到空响应?
我有用 python 编写的 Azure 函数应用程序。应用程序在天蓝色编辑器中运行良好,并且从 python 调用请求也运行良好。但是当我想从客户端 javascript (从我的 React 应用程序)调用它时,我得到空响应,
我以这种方式调用它:
fetch("https://xxxxx.azurewebsites.net/api/testtrigger?", {
mode: 'no-cors',
method: 'GET'
}).then(response => response.text()).then((response) => {
console.log(response)
})
no-cors 在那里,因为我在本地主机上测试它。
函数代码如下所示:
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Function called')
return func.HttpResponse("Its working")
Azure 显示正在调用该函数,但是我是否有任何原因错过了为什么这不起作用?我收到响应,但它是空的,这里是响应 json:
Response {type: 'opaque', url: '', redirected: false, status: 0, ok: false, …}
body: (...)bodyUsed: falseheaders: Headers[[Prototype]]: Headersappend: ƒ append()delete: ƒ delete()entries: ƒ entries()forEach: ƒ forEach()get: ƒ ()has: ƒ has()keys: ƒ keys()set: ƒ ()values: ƒ values()constructor: ƒ Headers()Symbol(Symbol.iterator): ƒ entries()Symbol(Symbol.toStringTag): "Headers"[[Prototype]]: Objectok: falseredirected: falsestatus: 0statusText: ""type: "opaque"url: ""[[Prototype]]: Response
Python 中的这段代码工作正常:
import requests
x = requests.get('https://xxxxx.azurewebsites.net/api/testtrigger')
print(x.text)
I have Azure function app written in python. App works in azure editor fine and in calling request from python also fine. But when I want to call it from client side javascript (from my react app) I get empty response
I call it in this way:
fetch("https://xxxxx.azurewebsites.net/api/testtrigger?", {
mode: 'no-cors',
method: 'GET'
}).then(response => response.text()).then((response) => {
console.log(response)
})
The no-cors is there because I test it on localhost.
Function code looks like this:
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Function called')
return func.HttpResponse("Its working")
Azure shows that function is being called, but is there any reason I missed why this should't work? I receive response but its empty, here is response json:
Response {type: 'opaque', url: '', redirected: false, status: 0, ok: false, …}
body: (...)bodyUsed: falseheaders: Headers[[Prototype]]: Headersappend: ƒ append()delete: ƒ delete()entries: ƒ entries()forEach: ƒ forEach()get: ƒ ()has: ƒ has()keys: ƒ keys()set: ƒ ()values: ƒ values()constructor: ƒ Headers()Symbol(Symbol.iterator): ƒ entries()Symbol(Symbol.toStringTag): "Headers"[[Prototype]]: Objectok: falseredirected: falsestatus: 0statusText: ""type: "opaque"url: ""[[Prototype]]: Response
This code in python works fine:
import requests
x = requests.get('https://xxxxx.azurewebsites.net/api/testtrigger')
print(x.text)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
no-cors
是对fetch
的显式指令,告诉它不要执行任何需要服务器使用 CORS 授予权限的操作。读取响应正文需要许可。
既然你说了
no-cors
你会得到一个空的正文而不是服务器响应的数据。no-cors
is an explicit directive tofetch
to tell it not to do anything that requires the server to use CORS to grant permission.Reading the response body requires permission.
Since you said
no-cors
you get an empty body instead of the data the server responded with.因此,巴西语的答案是 - 我无法从本地主机上的客户端浏览器调用Azure函数,我必须添加到我的软件包
中。
So bassicaly answer for this is - I can't call azure functions from client side browser on local host, I have to add to my package.json:
and fetch in with: