Coder Social home page Coder Social logo

Comments (8)

xiongwilee avatar xiongwilee commented on August 26, 2024

this.headers['content-type'] = 'application/json'就行了,但不建议这么搞;如果这是一个提交json数据的请求,最好写一个独立的controller:this.proxy('test:test/post')

from gracejs.

sophiabay avatar sophiabay commented on August 26, 2024

现在遇到一个很奇怪的问题。
在执行this.proxy()方法前,修改了 content-type ( this.headers['content-type'] = 'application/json' )

但到java服务器端,content-type 仍然是 "application/x-www-form-urlencoded"

我用postman测试后端接口时,就不会出现这种问题??

from gracejs.

xiongwilee avatar xiongwilee commented on August 26, 2024

先不管重设content-type的方案,修改下你的ajax,直接发送一个application/json的请求,然后直接proxy到后端试试?

from gracejs.

sophiabay avatar sophiabay commented on August 26, 2024

我通过 ajax的post方法, 设定 contentType:"application/json" 后

url设置为java后台请求地址时,结果正确。

但,如果使用 proxy 后,再到java后台,结果仍然不正确。

from gracejs.

sophiabay avatar sophiabay commented on August 26, 2024

貌似找到原因了。

在 co-proxy.js 中:

function requestWap(param, options, callback) {
// 获取request参数
let opt = Object.assign({
uri: undefined, // 请求路径
method: undefined, // method
headers: undefined, // 头信息
json: false, // 是否是json数据
gzip: true, //是否gzip
timeout: 15000 // 超时时间
}, {
form: param.ctx.request.body
}, options);

form: param.ctx.request.body 把这个参数去掉,在java后台就正确了

from gracejs.

sophiabay avatar sophiabay commented on August 26, 2024

let opt = Object.assign({
uri: undefined, // 请求路径
method: undefined, // method
headers: undefined, // 头信息
json: false, // 是否是json数据
gzip: true, //是否gzip
timeout: 15000 // 超时时间
}, {
body: param.ctx.request.body
}, options);

把 form: param.ctx.request.body 修改为 body: param.ctx.request.body后就可以了。
不知道对其它地方有没有影响呢?

from gracejs.

xiongwilee avatar xiongwilee commented on August 26, 2024

赞!我升级下,跑下测试看看!

from gracejs.

xiongwilee avatar xiongwilee commented on August 26, 2024

我试了一下,如果直接把body注入到request的boay参数里,如果这个请求是一个Stream就会出问题;

这里是这样:如果当前请求content-type是application/json,则服务器收到的肯定是application/json;这里不建议再做什么转换

from gracejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.