Pull CSV data from another domain

Pull CSV data from another domain

silkspinsilkspin Posts: 141Questions: 32Answers: 5

I can successfully import a CSV of data automatically from the same domain as the page loads, but nothing happens when trying to pull from another domain. I've seen the remote domain example and I'm aware it's to stop XSS, but it doesn't look exactly the same as my case. Can my working code below be successfully changed or will it be more complicated to achieve?

The .js

    $.ajax({
    url: "import.php",
    method: "POST",
    data: formdata,
    dataType: "json",
    contentType: false,
    cache: false,
    processData: false,
    success: function(jsonData) {
      $("#csv_file").val("");
      table = $("#datatable").DataTable({

import.php

<?php
 $file_data = fopen("data.csv", 'r');
 fgetcsv($file_data);
 while($row = fgetcsv($file_data))
 {
  $data[] = array(
   'date'  => $row[0],
   'name'  => $row[1],
   'address'  => $row[2],
   'town'  => $row[3]
  );
 }
 echo json_encode($data);
?>

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,292Questions: 26Answers: 4,768
    Answer ✓

    from the same domain as the page loads, but nothing happens when trying to pull from another domain.

    The best place to start is to use the browser's network inspector tool to see what response you get. This thechnote will provide steps to get you started.

    Kevin

  • silkspinsilkspin Posts: 141Questions: 32Answers: 5

    Thanks for answering. The web page loads without problems and says "No data available in table". In the dev tools Network > import.php > Response = Null. I don't get any errors under Network or in the Console.

    However, I have just tried to pull it from a different domain (without an SSL cert) and there wasn't a problem. The other remote domain which didn't work was a cPanel website with an SSL cert and fopen enabled. Are you aware of anything that might cause a problem?

This discussion has been closed.