0

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')
  }
});
imperium2335
  • 23,402
  • 38
  • 111
  • 190
  • 1
    Is there a reason you're doing this with an ajax call? `$.getScript()` is specifically for loading scripts.... http://api.jquery.com/jQuery.getScript/ – Reinstate Monica Cellio Oct 08 '13 at 10:04
  • @Archer Because it exposes too much of my path. – imperium2335 Oct 08 '13 at 10:05
  • `content-type : text/javascript` ? – mikakun Oct 08 '13 at 10:06
  • @mikakun It still cuts the script off half way through :( – imperium2335 Oct 08 '13 at 10:07
  • I fail to see how using `$.ajax` exposes anything extra? All your code is freely available as soon as you upload it. – Rory McCrossan Oct 08 '13 at 10:08
  • You can still use `$.getScript()` though. Just pass it a php url and get that to return the script, as you're doing with your ajax call in the code above. – Reinstate Monica Cellio Oct 08 '13 at 10:08
  • `echo file_get_contents($fullscriptpath)` instead of fopen/fread – mikakun Oct 08 '13 at 10:10
  • i agree the "too much exposure" is hmmm pointless – mikakun Oct 08 '13 at 10:12
  • If you have anything at all sensitive in your client-side code (JS) ask yourself why, and then remove it... if you're trying to do something with sensitive data then I'd say you're doing it very wrong, if you merely do not want to expose server-paths, then create a flat directory structure and use `$.getScript("script.js");` ... I reckon, though, that you're probably worried over nothing, all your code and file paths are visible to developer-tools. – simey.me Oct 08 '13 at 10:30

1 Answers1

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

header('Content-Type: text/javascript');
echo file_get_contents(WROOT . Vs . $script);
}
mikakun
  • 2,203
  • 2
  • 16
  • 24