Welcome Guest, Not a member yet? Register   Sign In
Calling controller function from javascript
#1

[eluser]Shiju S S[/eluser]
I have a URL working in the browser but get 404 error when called from the javascript function

I have a javascript function:
var xmlHttp55;

//////////////////////////
function thalukcheck(id){
document.getElementById("branch")[removed]="<img src='images/loading.gif' width='10' height='10' > Loading.......";
xmlHttp55=GetXmlHttpObject()
if (xmlHttp55==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="addfields/thaluk";
url=url+"/"+id;
//url=url+"&sid;="+Math.random();
alert (url);
xmlHttp55.onreadystatechange=stateChangedEff;
xmlHttp55.open("GET",url,true);
xmlHttp55.send(null);
}
function stateChangedEff()
{
if (xmlHttp55.readyState==4)
document.getElementById("branch")[removed]=xmlHttp55.responseText;
}
///////////////////
function GetXmlHttpObject()
{
var xmlHttp55=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp55=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp55=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp55=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp55;
}

The controller function is located in the localsite:
quickbharath/addfieldsdir/addfields/thaluk
addfields is the controller php
thaluk is the function accepting one parameter


The is the route
$route['addfields/thaluk/(:any)'] = "addfieldsdir/addfields/thaluk/$1";

#2

[eluser]pickupman[/eluser]
You may find using the jQuery library really easy to use, and makes writing AJAX request much easier and cleaner with it's cross browser support. That said, it looks like you have a set a relative path in your javascript.

Please use the [ code ] tag when posting code in the forums, as it will make it easier to read and copy. You may find changing the var url to:
Code:
var url="/quickbharath/addfields/thaluk";

I would also recommend the Firebug add on for Firefox. Enable the Console tab, and this will help you see all AJAX requests and responses. It's an invaluable tool for any developer.
#3

[eluser]Shiju S S[/eluser]
I am extremely thankful to you sir. I got it solved with the url correction.
#4

[eluser]pickupman[/eluser]
Glad it was something simple.




Theme © iAndrew 2016 - Forum software by © MyBB