0

I have ajax function in script as:

 $.ajax({

  url: 'http://www.somesitename.com/admin/exportToCSVAction',
  type: 'GET',
  data:{},
  cache: false,

  success: function() {
  alert("sucess");
  },
  error: function () {
  alert("error");
  }
  });

exportToCSVAction function in php as:

public function exportToCSVAction()
    {

        $exportBatch = 10;

        $order = $this->getTableGateway('order');

        $select = new Select();
        $select->from('order');



        $data = $order->selectWith($select)->toArray();

        $batchDir = __DIR__ . '/../../../../../data/export/batch/' . $exportBatch;
        mkdir($batchDir);

        $fileNameWithFilePath=$batchDir . '/order2.csv';

        if (file_exists($fileNameWithFilePath)) 
        {
            $this->downloadOrderCSVAction($fileNameWithFilePath);
        }
        else
        {

            $csvFile = fopen($batchDir . '/order2.csv', 'w');

            $i = 0;
            foreach($data as $record) {
                if($i==0) fputcsv($csvFile, $this->getCsvHeader($record));
                fputcsv($csvFile, $this->updateCsvLine($record));

                $i++;
            }

            fclose($csvFile);
        }
    }

But every time its returning me error as alert.

When i run it directly through link:

http://www.somesite.com/admin/exportToCSVAction

It returns me result correctly. (downloads the file as expected)

But through ajax it gives me Error as a alert.

Through inspect element network tab i get following:

enter image description here

Please help me.

Where i am making mistake?

Note:

I also tried by removing data from ajax, but no effect

Edit :

$.ajax({

      url: 'http://www.somesitename.com/admin/exportToCSV',
      type: 'GET',
      data:{},
      cache: false,

      success: function() {
      alert("sucess");
      },
      error: function () {
      alert("error");
      }
      });


exportToCSVAction function in php as:

    public function exportToCSVAction()
        {

            $exportBatch = 10;

            $order = $this->getTableGateway('order');

            $select = new Select();
            $select->from('order');



            $data = $order->selectWith($select)->toArray();

            $batchDir = __DIR__ . '/../../../../../data/export/batch/' . $exportBatch;
            mkdir($batchDir);

            $fileNameWithFilePath=$batchDir . '/order2.csv';

            if (file_exists($fileNameWithFilePath)) 
            {
                //Code to download file
            }
            else
            {

                $csvFile = fopen($batchDir . '/order2.csv', 'w');

                $i = 0;
                foreach($data as $record) {
                    if($i==0) fputcsv($csvFile, $this->getCsvHeader($record));
                    fputcsv($csvFile, $this->updateCsvLine($record));

                    $i++;
                }

                fclose($csvFile);
            }
        }

Now with this code i am getting the alet sucess. But not downloading the file

C Sharper
  • 8,284
  • 26
  • 88
  • 151
  • `alert("error");` is not informative at all. Log error code and text as mentioned in [docs](http://api.jquery.com/jquery.ajax/). – Regent Oct 09 '14 at 05:31
  • you do not need "data:" as you are using "GET" try once removing data – Neel Oct 09 '14 at 05:31
  • I also tried by removing data, but no effect – C Sharper Oct 09 '14 at 05:31
  • have access to the apache error log? – rogelio Oct 09 '14 at 05:35
  • Is ajax located on the same domain? Cross-domain ajax requests are forbidden due to security reasons. – Aleksei Matiushkin Oct 09 '14 at 05:36
  • ... and there are thousands of other reasons. Without info about error it is just guessing. – Regent Oct 09 '14 at 05:37
  • @mudasobwa yes ajax is located in same domain – C Sharper Oct 09 '14 at 05:38
  • Please post error_log snippet. – Vickrant Oct 09 '14 at 05:38
  • 2
    My guess is that http://www.somesitename.com/admin/exportToCSVAction is not valid – EternalHour Oct 09 '14 at 05:39
  • You need to check you http server rewrite rules or your php url rewriter (if u have one). 404 error mean that http://www.somesite.com/admin/exportToCSVAction is unreachable – Jevgenijs Vaikulis Oct 09 '14 at 05:50
  • You cannot download a CSV through AJAX. If you try, the body of the CSV is simply presented in the XHR object data member, which is not what you want. You would be better off by opening a new window, which with the proper headers would elicit a browser download prompt. Another, harder solution may be found in http://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post – LSerni Oct 09 '14 at 06:04

1 Answers1

0

You're getting a 404, so either there's a routing issue with your controller, or there's a fatal error somewhere in your controller action. One way to debug the controller action is try putting an error_log at the beginning of the exportToCSVAction function, something like:

error_log("BEGINNING OF FUNCTION");

Then check your apache error log and see if it logged the error. If it does log the error, then try putting another error_log statement after other statements inside the function and see if those are logged. You'll know which statement is causing the script to die if an error is not logged.