简体   繁体   English

axson无法使用Json服务器POST请求

[英]Json server POST request not working by axios

Hello Im using this json server https://github.com/typicode/json-server I can post data in postmman and its working good for example object 您好我正在使用这个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 To my server is running http://35.195.249.40:3004/users with header Content-Type: application/json我的服务器正在运行http://35.195.249.40:3004/users

And everything is ok 一切都很好

But on my app in nativescript/vue I cant make post request - its gives 304 response 但是在我的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);
            }); 

     }, 

I tried with fetch request too and it response 'parsing failed: ' [TypeError: Network request failed: java.io.IOException: Cleartext HTTP traffic to not permitted] 我也尝试了获取请求,它响应'解析失败:'[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)
                });

    },  

In the Postman example object you provided 在您提供的Postman示例对象中

{  
   "mainTab": {  
      "m_actual":0,
      "m_refresh":2000,
      "m_actual":0,
      "m_refresh":4000
     } 
  }

The fields are embedded in an object accessible via the mainTab key. 这些字段嵌入在可通过mainTab键访问的对象中。 However, in your actual code examples you are providing the object with the desired fields (like m_refresh ) directly and not in an embedded manner. 但是,在实际的代码示例中,您直接为对象提供所需的字段(如m_refresh ),而不是以嵌入的方式。

So, my suggestion is to wrap the object containing the data to make the posting call look like this: 所以,我的建议是包装包含数据的对象,使发布调用如下所示:

axios.post('http://35.195.249.40:3004/users', { mainTab: data },
  {
    headers:  { 
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    }
  }
)

these provided axios.post provide this code in response 这些提供的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\":...

I figured out axios would not work with http. 我发现axios不适用于http。 It needs https. 它需要https。 Is there possibility to make it working without https? 是否有可能在没有https的情况下使其工作?

other solution could be running json server with https. 其他解决方案可能是使用https运行json服务器。 Im using this server so what im doing im make npm start in ubuntu and server starts https://github.com/halfzebra/json-server-example 我正在使用这个服务器所以我正在做什么使得npm启动在ubuntu和服务器启动https://github.com/halfzebra/json-server-example

there is written on github to use https://github.com/typicode/hotel this to provide https but it working on localhost. 写在github上使用https://github.com/typicode/hotel这提供https但它在localhost上工作。 I need to run it on global IP over https 我需要通过https在全局IP上运行它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM