在user-dashboard中utils/request.js
export async function create(params) {
return request('/api/users', {
method: 'post',
body: qs.stringify(params)
});
}
这样传过去的参数,后台接收是context-type是text文本,我查了相关的资料,好像要自行设置为
export async function create(params) {
return request("/api/regexes", {
method: 'post',
body: JSON.stringify(params),
headers: {
"Content-Type": "application/json"
},
});
}
这样的话很麻烦,我还查了下,MDN这里有说
request 和response (也包括fetch() 方法)都会试着自动设置content type.如果没有设置Content-Type值,发送的请求也会自动设值.
我试了下,关没有自动设置content-type为json,如下:
export async function create(params) {
return request("/api/regexes", {
method: 'post',
body: JSON.stringify(params), //或者body:params,这样好像连参数都没有传过去
});
}
是我哪里操作不对么?这个总是本不应该出现在这里,我看示例上有用,请问,您有更好的解决方案么?
为啥要设 Content-Type,你的服务端要求必须设吗?
@sorrycc 后端说是要设,要不拿不到值。那如果后端要拿到json数据的话,用
body: JSON.stringify(params)
这样应该是可以的。
那就在 src/utils/request.js 里整体设下好了。
你好:
可以发出去的,只是后端解析方式不同,qs.stringify会把对象序列化成key=value&key=value这样,JSON.stringify的话会把对象序列会成JSON字符串。具体可以用devtools看下request。
在 2016年11月7日,下午5:21,Just a tech. 1991. [email protected] 写道:
@275287902 https://github.com/275287902 请问为什么用qs.stringify,body就发不出去呢?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/dvajs/dva/issues/79#issuecomment-258784362, or mute the thread https://github.com/notifications/unsubscribe-auth/AG9FAIOYLmtNQ6qH6WcPxnYEtQiDlSpdks5q7u1-gaJpZM4J6yRD.
@275287902 哈哈,谢谢,是我搞错了,qs.stringify 当value是undefined,时序列化就不存在了,没注意
@275287902
export async function create(params) {
return request('/api/users', {
method: 'post',
body: qs.stringify(params)
});
}
我也是这样写,为什么后台 $_POST 获取不到值呀?
@dan-me 换 post form 的方式看看? https://www.npmjs.com/package/whatwg-fetch#post-form
@sorrycc 我不是用form表单提交东西,我是随便传个params, get方式提交的的php后台可以通过$_GET拿到值,post方式提交的 $_POST 是拿不到值的 怎么办?
默认用的数据流的方法, 用$_POST自然取不到, 要用 file_get_contents(php://input)
@lincenying 谢谢 搞定了
Most helpful comment
那就在
src/utils/request.js里整体设下好了。