Calling a controller from external Javascript adds a "<" to the controller name

Hello,
i have this line in a javascript:

xmlhttp.open(“GET”,“<?=$this->Url->build(['prefix'=>false,'controller'=>'Terms','action'=>'searchterm?q='])?>”+str,true);

This works if I put the Javascript directly in the Header of the layout.
If i save the script in an external file in folder “webfoot/js” and fetch the script in my layout with

<?= $this->HTML->script(['scripts']) ?>
<?= $this->fetch('script') ?>

I get this reponse in the console of firefox:

<Controller could not be found.

> GET
> 	http://localhost/term1/customer/admin/%3C?=$this->Url->build(['prefix'=>false,'controller'=>'Terms','action'=>'searchterm?q='])?>t

Where does the additional “<” come from?

Your external script is being loaded directly as JS code, not executed as PHP. As you can see from what you’ve shown us here, the URL that’s being retrieved is quite literally

%3C?=$this->Url->build(['prefix'=>false,'controller'=>'Terms','action'=>'searchterm?q='])?>

%3C is ASCII encoding for <.

1 Like

Thank you for the help!

I have the similar proplem. Javascript files are routing to the Controller