cross-domain

 1   // GET
 2   jsonp = () => {
 3     let url = "http://xxx";
 4     fetch(url).then(function (response) {
 5       return response.json()
 6     }).then((json) => {
 7       console.log('parsed json', json);
 8       document.getElementById("demo").innerHTML = json.result[0].title;
 9     }).catch(function (ex) {
10       console.log('parsing failed', ex)
11     })
12   };
13   jsonp()
 1   // POST
 2   jsonp = () => {
 3     let postData = {
 4       account: 'xxx',
 5       password: 'xxx',
 6       name: 'xx',
 7       url_back: 'http://xxxxx'
 8     };
 9     fetch('http://xxxxx', {
10       method: 'POST',
11       mode: 'cors',
12       credentials: 'include',
13       headers: {
14         'Content-Type': 'application/x-www-form-urlencoded'
15       },
16       body: JSON.stringify(postData)
17     }).then(function (response) {
18       console.log(response);
19     });
20   };
21   jsonp()

猜你喜欢

转载自www.cnblogs.com/ronle/p/10753655.html