Question

I am trying to load a script using AJAX with the following:

$.ajax({
    type: 'POST',
    async: false,
    url: ROOT + 'Ajax',
    data: {
      call: 'this->loadJs',
      script: 'Activity2/js/buying.js'
    },
    dataType: "script",
    success: function(data) {
      alert('test')
    }
})

But I keep getting parseerror. Upon viewing the response I can see that the script is only partially loaded.

I thought this had something to do with filesize in my loadJs method (PHP) as defined here:

public function loadJs($script) // For our new $.getScript stuff.
  {
    $fh = fopen(WROOT . Vs . $script, 'r');
    $contents = fread($fh, filesize(Vs . $script));
    header('Content-Type: application/javascript');
    return $contents;
  }

But the file size is correct, so I am lost.

What is causing ajax to only partially load my script.

BTW, the following works, but I don't want to use it because it exposes too much of my :

$.ajax({
  url: 'http://www.view.com/public_html/views/Activity2/js/buying.js',
  dataType: "script",
  success: function(){
    alert('test')
  }
});
Was it helpful?

Solution

public function loadJs($script) // For our new $.getScript stuff.
{

header('Content-Type: text/javascript');
echo file_get_contents(WROOT . Vs . $script);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top