0

I'm trying to retrieve data from a webserver that I have no control over. I've tried google, but nothing seems to fix it. I've tried a few different codes, both with and without DataTables.

If someone could point me in the right direction I'd be extremely grateful.

 <script>
$(document).ready(function() {
$('#example').DataTable( {
    "ajax": {
        "url": 'https://Example.com',
        "dataSrc": "",
        "dataType": "jsonp"
    },
    "columns": [
        { "data": "name" },
        { "data": "description" }
    ]
} );
} );
</script>

When I run the code on the webpage in chrome, in developer tools/network the result to the url request I get

[{"ID":10,"VIRTUAL":"No","QTY":2,"NAME":"Alto TS Sub 15\"","ALT_NAME":"Alto TS15","DESCRIPTION":"","MEMO":"","PRICEA":0,"PRICEB":0,"PRICEC":0,"PRICE_TYPE":"per Day","TAX_RATE":"0%","STATUS":"Active","CATEGORY":"Speakers","CATEGORY_ID":9,"CATEGORY_LFT":18,"CATEGORY_RGT":19,"IMAGE_ID":101,"IMAGE_URL":"https:\/\/www.hirehop.info\/uploads\/Alto-TS-Sub-15in\/1758_101.png","REPLACE_COST":300,"WEIGHT":"31 kg","WIDTH":0.451,"LENGTH":0.513,"HEIGHT":0.591,"SERVICE_INTERVAL":"P6M","PAT_INTERVAL":"P1Y","TEST_INTERVAL":"P6M","LOCATION":"Greenway Avenue","PART_NUMBER":"","BARCODE":"","LAST_UPDATE":"2019-09-30 21:42:15","MARKETING":1,"NOMINAL_CODE":"","LINKS":[],"CUSTOM_FIELDS":[]},{"ID":9,"VIRTUAL":"No","QTY":2,"NAME":"Electro-Voice ZLX-12P","ALT_NAME":"EV-ZLX12P","DESCRIPTION":"12\" Active PA Speaker","MEMO":"","PRICEA":0,"PRICEB":0,"PRICEC":0,"PRICE_TYPE":"per Day","TAX_RATE":"0%","STATUS":"Active","CATEGORY":"Speakers","CATEGORY_ID":9,"CATEGORY_LFT":18,"CATEGORY_RGT":19,"IMAGE_ID":100,"IMAGE_URL":"https:\/\/www.hirehop.info\/uploads\/Electro-Voice-ZLX-12P\/1758_100.png","REPLACE_COST":350,"WEIGHT":"15.6 kg","WIDTH":0.35,"LENGTH":0.35,"HEIGHT":0.65,"SERVICE_INTERVAL":"P6M","PAT_INTERVAL":"P1Y","TEST_INTERVAL":"P3M","LOCATION":"Greenway Avenue","PART_NUMBER":"","BARCODE":"3","LAST_UPDATE":"2019-09-30 21:27:27","MARKETING":1,"NOMINAL_CODE":"","LINKS":[],"CUSTOM_FIELDS":[]}]
elliott37
  • 1
  • 1

1 Answers1

1

You said:

"dataType": "jsonp"

… but the URL returns JSON, not JSONP.

You have to use the dataType that matches the type of data you are getting (or leave it out entirely and trust that the Content-Type header on the response is correct).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • without "dataType": "jsonp", I get this in the console log. Access to XMLHttpRequest at 'https://myhirehop.com/modules/stock/stock_export.php?id=1758&key=vwymle5ix1wt&depot=1&cat=0&sidx=TITLE&sord=asc&_=1584455402104' from origin 'https://bsgproductions.co.uk' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. – elliott37 Mar 17 '20 at 14:31
  • @elliott37 — Which is normal unless bsgproductions grants you permission. See https://stackoverflow.com/a/35553666/19068 – Quentin Mar 17 '20 at 14:32