jquery - best practice to use action url while calling ajax in cakePHP -


I am using AJAX in my cake PHP application with jQuery.
and my javascript function is placed inside the javascript file.

Now the files in my local system are placed in the "/ sample" directory, so when I call the function

in ajax.js

  $ Post ({url: "/ sample / controller / action"})  

But after hosting it the URL will be created

  In PHP, we get the  $ html-> Url  $ .post to generate url ({url: "/mydomain.com/controller/action"})  

But since this code is in JS file then I can not use that function

I do not want to change all AJAX action URLs manually before hosting

What to do in your master template for your cake application Create a global javascript variable during your application Ensure that the goods can be sure to include it in any JS.

  & lt; Head & gt; ... & lt; Script type = "text / javascript" & gt; Var myBaseUrl = '& lt ;? Php echo $ html- & gt; Url; ? & Gt; '; & Lt; / Script & gt; ... & lt; Script type = "text / javascript" src = "mycustomJSfile.js" & gt; ... & lt; / Head & gt;  

Now you can do such things from any visual file in your MVC Framework app.

  $ Post ({url: myBaseUrl + Administrator / Action '});  

Comments

Popular posts from this blog

paypal - How to know the URL referrer in PHP? -

wpf - Line breaks and indenting for the XAML of a saved FlowDocument? -

oauth - Facebook OAuth2 Logout does not remove fb_ cookie -