我正在尝试从 php 文件中获取值
api.php
<?php
// want to fetch this value
$a = 'come!! fetch this value';
?>
Run Code Online (Sandbox Code Playgroud)
我的javascript页面有这样的代码。(此代码不在页面api.php上)
fetch('http://localhost/react_task/react-webpack-boilerplate/php/api.php', {
method: 'get',
// may be some code of fetching comes here
}).then(function(response) {
if (response.status >= 200 && response.status < 300) {
return response.text()
}
throw new Error(response.statusText)
})
.then(function(response) {
console.log(response);
})
Run Code Online (Sandbox Code Playgroud)
你能指导我如何使用 fetch 从 php 文件中获取变量的值吗?
我是第一次使用Fetch API 。它是否有类似于 jQuery 的ajaxSetup()方法的东西,以便为将来的请求设置授权标头?我找不到任何证据表明它确实如此。
我有以下示例测试:
import { assert } from 'chai'
function starWarsMovies () {
fetch('http://swapi.co/api/films/')
.then((res) => {
return res.json()
})
.then((res) => res.count)
}
describe('Get star war movies', () => {
it('should get 7', () =>{
assert.equal(starWarsMovies(), 7)
})
})
Run Code Online (Sandbox Code Playgroud)
但我得到
ReferenceError: fetch is not defined
Run Code Online (Sandbox Code Playgroud)
我必须使用什么才能测试提取请求。
更新
我也尝试过:
import { polyfill } from 'es6-promise'
import fetch from 'isomorphic-fetch'
Run Code Online (Sandbox Code Playgroud)
但后来我得到:
AssertionError: expected undefined to equal 7
Run Code Online (Sandbox Code Playgroud)
我不明白为什么。
我有一个像这样的函数:
check_auth(){
fetch(Urls.check_auth(), {
credentials: 'include',
method: 'GET'
}).then(response => {
if(response.ok) return response.json();
}).then(json => {
return json.user_logged_in;
});
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试这样做:
if(this.check_auth()){
// do stuff
} else {
// do other stuff
}
Run Code Online (Sandbox Code Playgroud)
但是,this.check_auth()始终是undefined。
我在这里缺少什么?我认为在 fetch 中then()是解析的Promise 对象所在的位置,因此我认为true当用户登录时我会得到。但事实并非如此。
任何帮助将不胜感激。
我有一些 json 数据,已使用 $.ajax 发布到 API,但我想更新它以使用 fetch API。不过,我似乎设置了 Fetch API 请求最终返回 403,所以我一定错过了一些东西,但我无法解决。
阿贾克斯请求:
$.ajax({
type: 'POST',
url: url,
data: {
'title': data.title,
'body': data.body,
'csrfmiddlewaretoken': csrf_token,
'request_json': true
},
success: function (data) {
console.log(data)
}
});
Run Code Online (Sandbox Code Playgroud)
获取尝试(众多尝试之一):
let payload = {
'title': data.title,
'body': data.body,
'csrfmiddlewaretoken': csrf_token,
'request_json': true
}
let request = new Request(url, {
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: JSON.stringify( payload )
});
fetch(request)
.then((response) => {
if (!response.ok) {
throw Error(response.statusText);
}
return response; …Run Code Online (Sandbox Code Playgroud) 我一直在为 freecodecamp 做随机报价机项目,我似乎无法停止缓存 api 的响应。
我尝试过各种答案:
我以为我曾经有过这样的经历:
fetch(this.url, {cache: 'no-cache'})
.then(res => res.json())
Run Code Online (Sandbox Code Playgroud)
...但我只是在开发人员工具中禁用缓存。
请帮忙。:-D。
这是我一直试图让它发挥作用的地方: https ://codepen.io/JonathanDWood/pen/gRNNKx
当前是否有任何浏览器支持在 HTTP/2 响应中访问预告片?
例如,最新的 Fetch API 规范提到预告片是通过 Response.trailer 公开的。有支持该功能的浏览器吗?
我刚刚迁移到 .NET Core 3.1,在我必须更新的不同内容中,我无法让我的 ajax 调用工作。
当我进行 ajax 调用时,没有任何数据绑定到我的操作中的对象参数。
这就是我在剃须刀页面中拨打电话的方式
fetch('/Administration/UpdateUserStatus', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'RequestVerificationToken': $('input[name="__RequestVerificationToken"]').val()
},
body: JSON.stringify(data)
})
Run Code Online (Sandbox Code Playgroud)
我的控制器动作
[HttpPost]
public IActionResult UpdateUserStatus([FromBody]User user) {
...
}
Run Code Online (Sandbox Code Playgroud)
在我上面的示例代码中,调试我可以看到user作为传入的代码null,因此来自 ajax 调用的数据不会绑定到User对象。
如果我删除[FromBody],user不会再进来null,但数据仍然没有绑定。所有属性为 null 或默认值。
在 .NET Core 2.1 中一切正常。
这是我需要添加的东西Startup.cs吗?也许我缺少一个新配置?
谢谢
在我的 React Native 0.62.2 应用程序中,IMG_1885.jpg为了测试目的,使用 fetch从云对象存储中获取图像:
let img = await fetch(bgimage.uri, {
method:"GET",
'Content-Type': 'image/jpeg'
});
Run Code Online (Sandbox Code Playgroud)
这是http返回并分配给变量img:
'img in sp ', { type: 'default', //<<<==output of img
? status: 200,
? ok: true,
? statusText: undefined,
? headers:
? { map:
? { 'x-oss-storage-class': 'Standard',
? 'accept-ranges': 'bytes',
? 'content-md5': 'Au4QpWK3O8+l4qCYxDjzw==',
? 'content-length': '475266', //<<<==size of file
? connection: 'keep-alive',
? 'content-type': 'image/jpeg', //<<<<==jpeg
? 'x-oss-server-time': '20',
? 'x-oss-object-type': 'Normal',
? date: 'Sun, 05 Jul 2020 …Run Code Online (Sandbox Code Playgroud) 我正在尝试向此端点发送请求:https://9gag.com/v1/group-posts/group/default/type/trending获取 9gag 帖子数据。
在邮递员和 chrome 上运行良好,但是当我尝试axios从 Node.js使用时,它响应403(并读取返回的 DOM,它认为它要求我输入验证码),当我尝试使用 发送请求时node-fetch,它响应与200帖子数据(这是正确的)。
问题是,为什么node-fetch工作正常但axios没有?
这是我用于测试的代码:
import axios from "axios";
import fetch from "node-fetch";
const URL = "https://9gag.com/v1/group-posts/group/default/type/trending";
// Inside async function
await fetch(URL); // Responded with 200 and json data
await axios(URL); // Responded with 403 and HTML DOM, axios.get() also gives the same result
Run Code Online (Sandbox Code Playgroud) fetch-api ×10
javascript ×8
ajax ×3
.net-core ×1
asp.net-core ×1
asp.net-mvc ×1
axios ×1
browser ×1
chai ×1
fetch ×1
get ×1
http2 ×1
mocha.js ×1
node-fetch ×1
node.js ×1
php ×1
react-native ×1
testing ×1
vue.js ×1