[英]Json server POST request not working by axios
您好我正在使用這個json服務器https://github.com/typicode/json-server我可以在postmman中發布數據並且它可以用於示例對象
{
"mainTab":{
"m_actual":0,
"m_refresh":2000,
"m_actual":0,
"m_refresh":4000
}
}
with header Content-Type: application/json
我的服務器正在運行http://35.195.249.40:3004/users
一切都很好
但是在我的nativescript / vue應用程序中,我無法發布帖子請求 - 它給出了304響應
updateJsonData(){
const data = {
m_actualHP: 0,
m_refreshHP: 2000,
m_actualMP: 666666,
m_refreshMP: 4000,
}
axios.post('http://35.195.249.40:3004/users', data, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
},
我也嘗試了獲取請求,它響應'解析失敗:'[TypeError:網絡請求失敗:java.io.IOException:Cleartext HTTP流量不允許]
updateJsonData(){
fetch('http://35.195.249.40:3004/users', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
"title": "Add a blogpost about Angular2",
"dueDate": "2015-05-23T18:25:43.511Z",
"done": false
})
}).then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json: ', json)
}).catch(function(ex) {
console.log('parsing failed: ', ex)
});
},
在您提供的Postman示例對象中
{
"mainTab": {
"m_actual":0,
"m_refresh":2000,
"m_actual":0,
"m_refresh":4000
}
}
這些字段嵌入在可通過mainTab
鍵訪問的對象中。 但是,在實際的代碼示例中,您直接為對象提供所需的字段(如m_refresh
),而不是以嵌入的方式。
所以,我的建議是包裝包含數據的對象,使發布調用如下所示:
axios.post('http://35.195.249.40:3004/users', { mainTab: data },
{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}
)
這些提供的axios.post提供此代碼作為響應
{ data: '',
JS: status: null,
JS: statusText: '',
JS: headers: {},
JS: config:
JS: { adapter: { [Function: xhrAdapter] [length]: 1, [name]: 'xhrAdapter', [prototype]: [Object] },
JS: transformRequest: { '0': [Object] },
JS: transformResponse: { '0': [Object] },
JS: timeout: 0,
JS: xsrfCookieName: 'XSRF-TOKEN',
JS: xsrfHeaderName: 'X-XSRF-TOKEN',
JS: maxContentLength: -1,
JS: validateStatus: { [Function: validateStatus] [length]: 1, [name]: 'validateStatus', [prototype]: [Object] },
JS: headers:
JS: { Accept: 'application/json',
JS: 'Content-Type': 'application/json' },
JS: method: 'post',
JS: url: 'http://35.195.249.40:3004/users',
JS: data: '{"data":{"mainTab":{"m_actualHP":0,"m_refreshHP":2000,"m_actualMP":666666,"m_refreshMP":4000}}}' },
JS: request:
JS: { UNSENT: 0,
JS: OPENED: 1,
JS: HEADERS_RECEIVED: 2,
JS: LOADING: 3,
JS: DONE: 4,
JS: _responseType: '',
JS: textTypes:
JS: [ 'text/plain',
JS: 'application/xml',
JS: 'application/rss+xml',
JS: 'text/html',
JS: 'text/xml',
JS: ...
JS: 'CONSOLE LOG END | CONSOLE DIR STARTS'
JS: ==== object dump start ====
JS: data: ""
JS: status: "null"
JS: statusText: ""
JS: headers: {}
JS: config: {
JS: "transformRequest": {},
JS: "transformResponse": {},
JS: "timeout": 0,
JS: "xsrfCookieName": "XSRF-TOKEN",
JS: "xsrfHeaderName": "X-XSRF-TOKEN",
JS: "maxContentLength": -1,
JS: "headers": {
JS: "Accept": "application/json",
JS: "Content-Type": "application/json"
JS: },
JS: "method": "post",
JS: "url": "http://35.195.249.40:3004/users",
JS: "data": "{\"data\":{\"mainTab\":{\"m_actualHP\":0,\"m_refreshHP\":2000,\"m_actualMP\":666666,\"m_refreshMP\":4000}}}"
JS: }
JS: request: {
JS: "UNSENT": 0,
JS: "OPENED": 1,
JS: "HEADERS_RECEIVED": 2,
JS: "LOADING": 3,
JS: "DONE": 4,
JS: "_responseType": "",
JS: "textTypes": [
JS: "text/plain",
JS: "application/xml",
JS: "application/rss+xml",
JS: "text/html",
JS: "text/xml"
JS: ],
JS: "_listeners": {},
JS: "_readyState": 4,
JS: "_options": {
JS: "url": "http://35.195.249.40:3004/users",
JS: "method": "POST",
JS: "headers": {
JS: "Accept": "application/json",
JS: "Content-Type": "application/json"
JS: },
JS: "content": "{\"data\":...
我發現axios不適用於http。 它需要https。 是否有可能在沒有https的情況下使其工作?
其他解決方案可能是使用https運行json服務器。 我正在使用這個服務器所以我正在做什么使得npm啟動在ubuntu和服務器啟動https://github.com/halfzebra/json-server-example
寫在github上使用https://github.com/typicode/hotel這提供https但它在localhost上工作。 我需要通過https在全局IP上運行它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.