我从节点服务器发送请求到其他服务器,但我需要发送内容类型
应用/ JSON
我如何发送,我正在使用此格式
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
//Do your fragment refreshing here
mCurrentPage = position;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
@Override
public void setFragmentsForContents(List<Content> contents) { //bundle data for your different fragments
if(contents!=null){
NUM_PAGES = contents.size();
mContentPager.addOnPageChangeListener(viewPagerPageChangeListener);
mContentPager.setAdapter(mScreenSlidePagerAdapter);
mContentPager.setCurrentItem(mCurrentPage);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ContentFragment.newInstance(mContents.get(position).getContent());
}
@Override
public int getCount() {
return NUM_PAGES;
}
@NonNull
@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
@Override
public float getPageWidth(int position) {
return 1.0f;
}
}
我在尝试这个时遇到错误
request.post('https://server.com/index.php/rest/V1/integration/admin/token',{form:postData},function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
res.json({
'error': error,
'statusCode': response && response.statusCode,
'body': body
})
});
“message”:“服务器无法理解Content-Type HTTP标头媒体 键入application / x-www-form-urlencoded“
答案 0 :(得分:6)
请将密钥名称
form
更改为json
。
request.post('https://server.com/index.php/rest/V1/integration/admin/token', {
json: postData
}, function(error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
res.json({
'error': error,
'statusCode': response && response.statusCode,
'body': body
})
});
答案 1 :(得分:2)
根据the docs,您需要将json:true
的{{1}}设置为添加到标题
application/json
答案 2 :(得分:-1)
见the documentation。您只需要在选项对象中包含headers
属性的值:
request.post(
'https://server.com/index.php/rest/V1/integration/admin/token',
{
form: postData, /* Ensure this is a string of JSON! */
headers:{
"Content-Type": "application/json"
}
},
your_callback_function
);
答案 3 :(得分:-1)
修正了这个问题,
request.post({
url: url,
method: "POST",
headers: {
"content-type": "application/json",
},
json: postData
},function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
res.json({
'error': error,
'statusCode': response && response.statusCode,
'body': body
})
});