Hi I have a jQuery ajax request for a login system. At first, it worked very well. But after a few try, it just started to show negative response. I checked firebug and it says that the response is, in my case "Connected". But the ajax response just shows "Not_connected". I don't know what to do :(. Please help me.
This is my jquery code :
var data_str = "username="+usrn+"&password="+pwd;
$.ajax({
type: "POST",
url: "index.php?rnd=" + Math.random(),
data : data_str,
complete : function(xhr,data){
if(data == 'connected'){window.location.href = 'admin.php';}
else if(data = 'not_connected'){ error_gen.html('Invalid username or password'); }
alert(data);
}
});
AS for the PHP code :
$log_result = $u_obj->login_user();
if($log_result == true)/*user is connected*/
{
echo 'connected';
exit;/*stoping the script after sending the result*/
}
elseif($log_result == false)/*error while logging in*/
{
echo 'not_connected';
exit;/*stoping the script after sending the result*/
}
Look at this thread: Is it possible to cache POST methods in HTTP?
It might be that there are headers which now make browser caching the response (although it's POST).
Also instead of rnd=" + Math.random() you can add write
$.ajax({
type: "POST",
cache: false,
..
Could it be browser caching?
Try adding this $.ajaxSetup({ cache: false });
You are using the wrong $.ajax option to retrieve the result. You should use the success option. Just change the
complete : function(xhr,data){
line for
success : function(data){
It should work.
Related
i've got a little question regarding the usage of Ajax. I've done a search for the same issues but i can't find something to look alike with my problem.
Here is my code.
$(document).ready(function()
{
$("#nume").blur(function() {
numeform = $("#nume").val();
if (numeform){
$.ajax({
type: "POST",
url: "../../index.php",
data: {numeform : numeform},
cache: false,
success: function(response) {
data = numeform;
console.log(data);
}
});
}
});
});
and in the php file i have
$data = $_POST['data'];
echo $data;
The console shows as it should. Any help is greatly appreciated.
The console is logging the variable you are sending not what you're receiving, that is why it looks correct. If you log response then you'd see a problem.
For the PHP part you're reading $_POST['data'] when you should be reading $_POST['numeform'] becuse that is what you sent in your ajax request
{numeform : numeform}
I have this script:
<script type="text/javascript">
jQuery(document).on("click",".pending-post-approve-link",function(e){
jQuery('#review_post_status').val('approve');
var globalVar = jQuery(this).find('.review_post_id').val();
jQuery('#review_post_current_id').val(globalVar);
jQuery('#post-preloader-'+globalVar).fadeIn(500);
$.fn.wpjobusSubmitFormFunction();
});
$.fn.wpjobusSubmitFormFunction = function() {
var globalVar = jQuery('#review_post_current_id').val();
jQuery('#wpjobus-pending-posts').ajaxSubmit
({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
data: jQuery('#wpjobus-pending-posts').serialize(),
type: "POST",
success: function(response)
{
jQuery('#post-preloader-'+response).fadeOut(100);
jQuery('#post-'+response).fadeOut(100, function()
{
jQuery(this).css('display', 'none');
});
return false;
}
});
}
});
</script>
When this script runs, it gives me empty response. I don't know what causing this problem. This is happening in the success function of the ajax success: function(response).
Any help would be appreciated.
You might have done some of those steps, but myself I would take this approach to find the issues:
Use a tool such as Google Chrome console or Firebug for FF to check if your request is being sent to the expected url, and that it is getting an expected response.
If it is not getting the expected response, the issue is serverside and you should look there. You can also make some breakpoint using the same tools (console/firebug) to see if the parameters which you are passing look as expected (you can check the POST header details instead too).
Finally you can try placing a breakpoint on the success function to be sure that it gets executed at all and that the response is an empty string, and not something else.
I hope this helps.
I'm using Jquery ajax to check registration form.
this is my code:
$("input.register_input").each(function() {
name= this.name;
$(".ono#"+name).html("<img src='images/ajax-loader.gif'>");
if (name == 're_password') {
var dts = this.name+"="+$(this).val()+"&pass="+$("input[name='password']").val();
} else {
var dts = this.name+"="+$(this).val();
}
$.ajax({
type: "POST",
url: "ajc/register_check.php",
data: dts,
success: function(resultfrompage){
$(".ono#"+name).html(resultfrompage);
}
});
});
This is after user submitting the form. so I can't check all values at once.
I dont completly sure if that's the problem, but I this the each() loop is running before the ajax request is done so I'm getting only 1 value (last one) back. and all the rest still showing ajax-loader.gif.
This is the reason for the problem? and if so how can I fix it?
thank you!
Try t use async:false.
By default, all requests are sent asynchronously (i.e. this is set to
true by default). If you need synchronous requests, set this option to
false. Cross-domain requests and dataType: "jsonp" requests do not
support synchronous operation. Note that synchronous requests may
temporarily lock the browser, disabling any actions while the request
is active. As of jQuery 1.8, the use of async: false with jqXHR
($.Deferred) is deprecated; you must use the success/error/complete
callback options instead of the corresponding methods of the jqXHR
object such as jqXHR.done() or the deprecated jqXHR.success().
$("input.register_input").each(function() {
name= this.name;
$(".ono#"+name).html("<img src='images/ajax-loader.gif'>");
if (name == 're_password') {
var dts = this.name+"="+$(this).val()+"&pass="+$("input[name='password']").val();
} else {
var dts = this.name+"="+$(this).val();
}
$.ajax({
type: "POST",
async: false,
url: "ajc/register_check.php",
data: dts,
success: function(resultfrompage){
$(".ono#"+name).html(resultfrompage);
}
});
});
I suggest you to show some loading image while the Ajax request is processed. This way the user will understand that something is going on in the background. When this process will be finished handle the response.
The Ajax function below sends data from a page to the same page where it is interpreted by PHP.
Using Firebug we can see that the data is sent, however it is not received by the PHP page. If we change it to a $.get function and $_GET the data in PHP then it works.
Why does it not work with $.post and $_POST
$.ajax({
type: "POST",
url: 'http://www.example.com/page-in-question',
data: obj,
success: function(data){ alert(data)},
dataType: 'json'
});
if there is a problem, it probably in your php page.
Try to browse the php page directly in the browser and check what is your output.
If you need some inputs from post just change it to the GET in order to debug
try this
var sname = $("#sname").val();
var lname = $("#lname").val();
var html = $.ajax({
type: "POST",
url: "ajax.class.php",
data: "sname=" + sname +"&lname="+ lname ,
async: false
}).responseText;
if(html)
{
alert(html);
return false;
}
else
{
alert(html);
return true;
}
alax.class.php
<php
echo $_REQUEST['sname'];
echo $_REQUEST['sname'];
?>
Ajax on same page will not work to show data via POST etc because, PHP has already run that's why you would typically use the external page to process your data and then use ajax to grab the response.
example
success: function(){
$('#responseDiv').text(data);
}
You are posting the data... Check if the target is returning some data or not.
if it returns some data then only you can see the data otherwise not.
add both success and error.. so that you can get what exactly
success: function( data,textStatus,jqXHR ){
console.log(data);//if it returns any data
console.log(textStatus);//or alert(textStatus);
}
error: function( jqXHR,textStatus,errorThrown ){
console.log("There is some error");
console.log(errorThrown);
}
i wonder if this is true and safe way to send request via ajax json type to php file or not ?!
note : it return success result ..
but my question if to keep them like this or change it to another safe method ?!
Html Code
<span class="clickable" data-bind={"name":"master","tag":"1"}>click</span>
Javascript Code :
$(".clickable").livequery('click touchstart', function (e)
{
var bind = $(this).data("bind");
$.ajax({
type: "POST",
url: "page.php",
data: bind,
dataType: 'json',
success: function (response)
{
alert(response)
}
});
e.stopImmediatePropagation();
});
PHP File :
$name= mysql_real_escape_string(trim(htmlentities(strip_tags($_POST['name']))));
if(!isset($name) || $name == '' || $name != 'master') {
echo 'Error: Invalid Action';
exit;
}else{
// Do Something
}
Basicly whenever you use ajax to post to a page, anyone with a developer console in their browser is able to see that request. And alter it as they like. Just like they can alter the data-bind attribute by using something like firebug.
You should implement checks on the POST variables in your page.php to make sure the input is not something you want inserted into your PHP code.