简体   繁体   中英

Yii Ajax form submition without inline script

I have an app in Yii and i want to ajax form submition without inline script unlike this link said, because inline script is bad for SEO.

So i create php file and put my js+php code in it with header

Header("content-type: application/javascript");

And include it as Javascript file. But java script return this error :

Fatal error: Class 'Yii' not found in /var/www/mydomain/js/sendmessage.php on line 16

Which line 16 contains:

var sendurl = request->getUrl() ?>+"/handler";

What should i do to make it work ?

Your problem is that Yii is not yet initiated in your file.

You should probably make a JsController or something like that, and let it serve the js content for you instead of just making one external file like that. That will ensure that you have all the classes loaded that you want.

I mean, you wouldn't create a PHP file like that for anything else on your website right? So why do quickfixes just because it's js.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM