问题
i have the following javascript in my view:
$.ajax({
url:"<?php echo site_url('switches/showknownvlans/'.$ip.'/'.$hardwaremodel);?>",
type:'POST',
dataType:'json',
success: function(returnDataFromController) {
//alert(returnDataFromController.length);
var htmlstring;
var submitFormHTML;
htmlstring = "some html stuff";
for(i = 0; i < returnDataFromController.length; i++) {
}
submitFormHTML = "<form method='post' accept-charset='utf-8' action='controllerX/methodABC/"+ $('#ip').val() +"/" + $('#hardwaremodel').val() +"/" + $('#port').val() + "'><input type='text' id='newVlanID' style='width:5em;height:1.5em'/> <button type='submit' class='btn' id='saveVlan' style='width:10em;height:2em'>Reassign Vlan</button></form>";
alert(submitFormHTML);
$('#clientajaxcontainer').html(htmlstring);
$('#newvlanform').html(submitFormHTML);
The path that I currently have defined for my form's "action" property in the "submitFormHTML" string is incorrect. Instead of taking my user to "http://myserver/myapp/controllerX/methodABC/" with all the parameters, its appending "controllerX/methodABC/" to the end of the current URL. so if the user is at:
http://myserver/mypp/controller23/method123/
when the click on the button to submit the form, they end up at:
http://myserver/mypp/controller23/method123/controllerX/methodABC/
Is there a way to get either the site_url() or base_url in javascript? Any suggestions? Thanks for reading the post.
Edit:
As per someone's suggestion (I think it's a good idea) I've create a new .js file called "global.js" and I have one line in it:
var BASEPATH = "<?php echo base_url(); ?>";
This file is then included in my template PHP file for my view like so:
<!DOCTYPE html> <!-- aka HTML5 -->
<html lang="en">
<head>
<meta charset="utf-8">
<link href="<?php echo base_url();?>assets/css/bootstrap.css" rel="stylesheet">
<script type='text/javascript' src='<?php echo base_url();?>assets/js/global.js' charset="utf-8"></script>
I've modified my javascript that craetes the form to look like: submitFormHTML = " Reassign Vlan"; alert(submitFormHTML);
Edit2:
What's interesting is that when I define BASEPATH in the global.js, the URL my javascript ends up generating looks like this:
http://myserver/myapp/index.php/switches/showportvlan/parm1/parm2/%3C?php%20echo%20base_url%28%29;?%3E/index.php/switches/changeportvlan/parm1/parm2/parm3.
As you can see, instead of interpretting the "", it just included the text as is.
But if i forget including a separate js file and just do this:
<link href="<?php echo base_url();?>assets/css/bootstrap.css" rel="stylesheet">
<script type='text/javascript'>
var BASEPATH="<?php echo base_url();?>";
</script>
it works just fine.
I can't see why the include file fails.
回答1:
Updated Answer to match your new question
header.php
<base href="<?php echo base_url(); ?>" />
<link href="assets/css/bootstrap.css" rel="stylesheet">
<script>
//global vars, can be used in any external js file
var BASEPATH = "<?php echo base_url(); ?>";
var IP = "<?php echo $_SERVER['REMOTE_ADDR']";
</script>
<script src="assets/js/global.js"></script>
global.js
(function($){
var formsObject = {
init : function(){
if($("form#id1"))
{
this.formOne; //run form one if id is present
}
},
formOne : function(){
var fOne = $("form#id1");
fOne.submit(function(){
var data = {
'' : '',
}
do_ajax(data);
});
var do_ajax = function(data){
$.ajax({
url : BASEPATH + fOne.attr('action'),
//and so on
});
}
}
}
//ready
$(function(){
formsObject.init();
});
})(jQuery);
回答2:
The action is actually being called correctly. Similarly to href attributes, if you start it without a slash (/
) it will just append the value to the current page's uri, which is the behaviour you describe.
You have 2 options to refer to this script from the root of the HTTP HOST:
start the action attribute with a slash:
action="/controllerX/methodABC/"
or add the full path to your server, including protocol:
action="http://myserver/mypp/controllerX/methodABC/"
来源:https://stackoverflow.com/questions/12302757/codeigniter-forms-action-property-how-to-specify-full-app-path-in-javascrip