I want to load simple codeignitier controller, but no success i get 404 error.
It looks like jquery load does not support pretty/clear urls? Is it possible and how?
When i try to load some file using this same code it works perfectly, but when i try to load codeignitier controller function nope, i get 404 in Console.
<script type="text/javascript">
$(document).ready(function () {
$("#season").load('https://www.example.co/index.php/system/example');
//i already tried without 'index.php'
});
</script>
when i check does load function works using some simple file, it works perfectly:
<script type="text/javascript">
$(document).ready(function () {
$("#season").load('https://www.example.co/test.php');
});
</script>
and controller:
public function example(){
echo "EXAMPLE";
}
This link exists and is not 404 (of course without example):
https://www.example.co/index.php/system/example
Try by pass it as
window.location.href = '<?php echo base_url('controller/method'); ?>'
I solve it by adding '?' before index.php.
So this is result:
<script type="text/javascript">
var PI = {
onReady: function() {
$.ajax({
type: 'GET',
url: '/index.php?/system/getseasons/SC0',
success: function (response) {
alert('success');
$("#season").html(response);
},
error: function(xhr, textStatus, error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
})
},
};
$( document ).ready( PI.onReady );
</script>
Related
When I perform a simple ajax request I get the following error
TypeError: 'toString' called on an object that does not implement interface HTMLAnchorElement.
This is my ajax script
<script type="text/javascript">
$(document).on("ready", function(){
$("#tabla_persona table tr").click(function() {
var cod = $( "#identificador",this);
alert(cod.html());
var parametros={
"cod":cod
};
$.ajax({
type: 'POST',
url: '<?php echo site_url("archivo/prueba"); ?>',
data: parametros,
success: function(resp) {
$("#tabla_usuario_individual").html(resp);
}
});
});
});
</script>
This is my controller
public function prueba(){
$this->load->view('datos_persona');
}
and my simple page to see the result
<a>
Prueba
</a>
Try changing:
var parametros={
"cod":cod
};
to:
var parametros={
"cod":cod.html()
};
Just need simple change.I think the following code will work.
public function prueba(){
echo $this->load->view('datos_persona',true);
}
This is rather an odd question, but, I want to make $.ajax fail to check my fail trigger in it, I have this jQuery code:
$(document).ready(function () {
$("#trigger").on("click", function () {
$.ajax({
url: "text.php",
success: function (data) {
$(".log").html(data.slice(0, 1000));
//alert("Load has been performed");
},
fail: function () {
$(".msg").html("Something went wrong...");
}
});
});
});
And am calling this PHP code:
<?php
function foo()
{
return "bar";
}
print foo();
return false;
?>
and want to make sure that the fail: ... is working. How do I do this?
EDIT
I have amended the text.php to be this:
<?php
throw new LogicException("Failed");
Also:
<?php
header("HTTP://this.does.not.exist");
But this still shows in the $(".log") area even though it has failed.
Noticed I was using the fail function wrong, now amended my jQuery to:
$(document).ready(function () {
$("#trigger").on("click", function () {
$.ajax({
url: "text.php"
}).done(function (data) {
$(".log").html(data.slice(0, 1000));
}).fail(function () {
$(".msg").html("Something went wrong...");
}).always(function () {
alert("One way or another, we did it!");
});
});
});
Also if it helps, I am using these scripts for jQuery libraries:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
You could send a header returning an error.
For example:
header("HTTP/1.0 404 Not Found");
Or of course have your script generate a fatal error ;-)
Two suggestions:
Change the .fail callback call to .error and
Try it by returning an error status code from your server like jeroen suggested,
header("not-real.php", true, 404);
I am trying to load content of a URL which is in a href of a Link using AJAX/JQUERY.
For some reason, the AJAX doesn't get executed and when i click on the Link, the URL will open in a new window!
but I need to load the content of URL on the same page that has the AJAX without opening a new window!
this is my current code:
<div id="content"></div>
<a id='listc' class='lisitem' href='file.php?id=".$id."' >".$id."</a>
and the AJAX:
<script type="text/javascript">
$("#listc").click(function() {
$("#content").load(this.href);
return false;
});
</script>
just to clarify, I do have the jquery lib included in my page header and I am using it for other stuff on the same page. so the issue is not that.
could some please advise on this issue?
Thanks
You need to prevent the default action of the link:
<script type="text/javascript">
$("#listc").click(function(e) {
e.preventDefault();
$("#content").load(this.href);
return false;
});
</script>
You need to do something with your AJAX return :
<script type="text/javascript">
$("#listc").click(function() {
$("#content").load(this.href, function(result){
alert("AJAX done");
});
return false;
});
</script>
You can change the href from the actual page, to a javascript: call. This way, you wouldn't have to concern about the default link redirect.
<a id='listc' class='lisitem' href=javascript:LoadFunction('file.php?id=".$id."'); >".$id."</a>
JS:
function LoadFunction(url) {
$("#content").load(url, function(result){
//something
});
}
Give the following a try:
document.getElementById('listc').addEventListener('click', function() {
$("#content").load(this.href);
return false;
});
UPDATE
Change:
<a id='listc' class='lisitem' href='file.php?id=".$id."' >".$id."</a>
to:
<a id="listc" class="listitem" data-id="<?= $id; ?>" href="#"> <?= $id; ?></a>
And AJAX to:
$(document).delegate('.listitme','click',(function() {
var id = $(this).data('id');
$('#content').load('file.php?id=' + id);
return false;
}
or:
$(document).delegate('.listitme','click',(function() {
var id = $(this).data('id');
$.post('file.php', {id : id}, function(answer) {
$('#content').html(answer);
});
}
Try something like that :
When you click on the link, do an AJAX call then print the result in your container.
<script type="text/javascript">
$("#listc").click(function(e) {
e.preventDefault();
$.ajax({
url: $(e.currentTarge).attr('href'),
complete: function () {
// remove loading status if neeed
},
success: function (response) {
$("#content").html(response)
},
error: function() {
//manage error here
}
});
});
</script>
Hi I have searched the web but can't get this to work. I'm trying to call the file databaseUpdated.php (placed in the same folder as the index.php file) from a function that is called every 10 seconds.
If I place the following in the index.php
<script type='text/javascript'>updateboolean();</script>;
the function is runned so thats not the problem, the problem is that the php file is not read.
the file databaseUpdated.php
<body>
<?php
echo ("<script type='text/javascript'>updateboolean();</script>;");
?>
</body>
And here is my functions in the javascript
$(document).ready(function(){
setInterval(function() {
$.get("databaseUpdated.php");//Can't get this to work any obvious reason for this (And yes I have jquery working)?
return false;
}, 10000);
});
function updateboolean(){
alert("Database updated");
document.location.reload(true);
}
Thanks in advance =)
Edit
_________________________________________________________________________________________
When I do
alert(data); in the below function I get the result as the image will show
$(document).ready(function(){
setInterval(function() {
$.get('databaseUpdated.php', function(data) {
alert('Load was performed.');
alert(data);
eval(data);
});
}, 5000);
});
But the "eval(data)"
doesn't seem to work
$.get("databaseUpdated.php"); // This will only return contents of that file The scripts in that file are not executed. To execute them you need to do eval(). So Try This
$.get('databaseUpdated.php', function(data) {
eval(data);
});
Also, may be you will require to change your php file as following:
echo ("updateboolean();");
where is your callback function for ajax
this is how it should be
$(document).ready(function(){
setInterval(function() {
$.get('databaseUpdated.php', function(data) {
alert('Load was performed.');
});
}, 10000);
});
Try this:
$(document).ready(function(){
setInterval(function() {
$.get('databaseUpdated.php', function(data) {
alert("Database updated");
// or alert(data); //in case you return data from php
document.location.reload(true);
});
}, 10000);
});
By doing $.get("databaseUpdated.php"), you request the PHP page, but ignore the return results.
Try this:
PHP
echo "updateboolean();";
JavaScript:
$.getScript("databaseUpdated.php");
try this in place of your echo
echo "<script>updateboolean();</script>";
I am trying to use jQuery's ajax to call a php script onload. This script will return xml from a url web service, parse through it, and then display bits and pieces of it into a div tag when the page loads (onload). I'm okay with php when it comes to using forms, but getting php scripts to run onload is not working for me. Could someone please look through my code and give me your advice? Thanks in advance.
HTML:
<!doctype html>
<html>
<head>
<title>Word of the Day</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="code.js"></script>
</head>
<body>
<h3>Word of the Day: </h3>
<div id="word_day"></div>
</body>
</html>
JavaScript:
$(document).ready(function() {
$(window).load(function() {
$.ajax({
post: "GET",
url: "word_day.php"
}).done(function() {
alert(this.responseText);
}).fail(function() {
alert(this.responseText);
});
});
});
I did not add my PHP code since I was pretty sure that it wasn't the cause of my frustration.
You don't need those two handlers, just one:
$(document).ready(function()
{
$.ajax(
{
post: "GET",
url: "word_day.php"
}).done(function()
{
alert(this.responseText);
}).fail(function()
{
alert(this.responseText);
});
});
As you had it you were trying to create one handler when the handler fired which was never going to work.
Edit:
Your done/fail part should look like:
}).done(function(data)
{
alert(data);
}).fail(function(jqXHR, textStatus, errorThrown)
{
alert(textStatus);
});