simple ajax call not working
vigneshwarraja opened this issue · comments
Hi,
I'm building a website on top of your framework and I must say I really love it, But there is one small issue that I'm facing I would like incorporate a Ajax login system in my website and when i try to do it I'm not getting a proper ajax response instead the i get the whole html code as response, below is the code,
JS
$('#button').on('click',function(e){
e.preventDefault();
var uname=$('#lgnEmail').val();
var password=$('#lgnPassword').val();
var dataString = 'uname='+ uname + '&password='+ password;
$.ajax({
type: "POST",
url: 'http://localhost/ajaxseo/xyz',
data: dataString,
crossDomain: true,
cache: false,
success: function(result) {
alert(result);
},
error: function(jqXHR, textStatus,err) {
alert(err);
}
});
});
XYZ PHP-File
echo "Hello";
I have added the corresponding line in your .htaccess file to make the file work and i have placed the xyz.php file in the '/content' folder exactly as api.php file
RewriteRule ^xyz(?:/([^.]*))?$ index.php?xyz&url=$1 [QSA,L]
I have tried with datatype:json & jsonp with callback parameters, I also tried using json_encode in the php.
I'm sure it has something to do with your .htacces file and I sincerely don't know anything about .htaccess, could you please help me out here?
RewriteRule ^xyz(?:/([^.]*))?$ index.php?xyz&url=$1 [QSA,L]
It works for me fine. Do not forget to add in your index.php
if (isset($_GET['xyz'])) {
include 'content/xyz.php';
exit;
}
and set in xyz.php what ever you like.
For callback query do not forget to use
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !/api(/.)??callback=([^&])
Try to use the latest AJAX SEO code and let me know if it work out for you.
haha rookie mistake I forgot to include
if (isset($_GET['xyz'])) {
include 'content/xyz.php';
exit;
}
Now it works fine, thank you so much. Besides that, I'm aware that your latest ajax-seo version now binds jquery address to the ajax loaded content, but our own custom js scripts are not binding to the ajax loaded content. say for example, in your demo at the history page content lets assume that there is a link like
<a href='#' id="xyz">Click me</a>
and we have JS
$('#xyz').on('click',function(e){
alert('Hello');
});
So when the history page is loaded via ajax and if we click on the 'Clickme' link nothing really happens, it seems our custom javascripts are not binding to the ajax loaded content, they work fine when the history page is loaded independently.
Is there any work around for this, currently I'm wrapping all my custom js scripts in a function and calling them once 'again' when the ajax call is completed.
Good to hear that everything works fine.
To bind event to Ajax loaded content, try method
$(document).on('click', '.js-as', function(e) {
console.log(e.target);
});