Request.JSONP

by Oski Krawczyk

HTML

<div class='wrapper'>
    <p>JSONP is loaded from different domain</p>
    <ul id='post'></ul>
</div>

CSS

body {
    font-family: Helvetica, Sans, Arial;
}
p, ul {
    padding: 10px;
}
ul {
    margin: 5px;
    border: 2px dashed #999;
}

JavaScript

new Request.JSONP({
    url: 'https://jsfiddle.net/echo/jsonp/',
    data: {
        text: 'some text',
        par1: 'another text'
    },
    onSuccess: function(response) {
        //show_response(response, document.querySelector('#post'));
    }
}).send();

/* fetch('https://jsfiddle.net/echo/jsonp/', {
  method: "post",
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
      text: 'some text',
      par1: 'another text'
    })
  }
)
.then((response) => response.text())
.then((responseText) => {
  //const match = responseText.match(/\?\((.*)\);/);
  //if (! match) throw new Error('invalid JSONP response');
  console.log(JSON.parse(responseText));
})
.catch((error) => {
  console.error(error);
}); */

show_response = function(obj, result) {
    result.innerHTML = `${obj.text} - ${obj.par1}`
}