For some reason I don't get response while using ajax requests. It doesn't work on Internet exploer and Opera. It works on Firefox and Chrome. Here is the code:
$(document).ready(function() {
$("#registration").submit(function (e) {
e.preventDefault();
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "/ajax.php",
data: str,
success: function (msg) {
alert(msg);
}
});
});
});
I added AddDefaultCharset utf-8 to .htaccess file but I still don't get it to work on IE and Opera.
What could be the problem?
In the past I faced the same issue. What solved it is by putting the .ajax call on a var.
var callAjax = function(){
$.ajax({
type: "POST",
url: "/ajax.php",
data: str,
success: function (msg) {
alert(msg);
}
};
Also try putting the following outside the jquery Click event. (Global)
var str = $(this).serialize();
I hope this helps.
Related
for some reason my $.ajax() is posting but not providing a success message which is rather strange because i am using the same code from another application just with different values
$("#submit").click(function(e)
e.preventDefault();
var email = $("#email_address").val();
var url = "<?=base_url()?>" + "account/validate_registration";
$.ajax({
type:"POST",
url:url,
data:{"email_address":email},
success: function(res,status){
alert(res);
}
});
});
Sometimes chrome developer console displays the response of the php file which is
print_r($_POST);
I cant understand how it works in one application and not in this one
$.ajax({
type:"POST",
url:url,
dataType: "json",
data:{"email_address":email},
success: function(res,status){
alert(res);
}
});
this url should return json data (var url = "<?=base_url()?>" + "account/validate_registration";)
I am trying to get a jQuery var into PHP so I can use it with mysql. I have searched everywhere but nothing seemed to solve it.
I have the following jQuery code:
$('.eventRow').click(function(){
var eventID = this.id;
$.ajax(
{
url: "index.php",
type: "POST",
data: { phpEventId: eventID},
success: function (result) {
console.log('success');
}
});
$('#hiddenBox').html(eventID);
console.log(eventID);
});
If I run this, the ID is shown in both #hiddenBox and in the console.log. The console also says "Succes" from the Ajax.
I am trying to get it in the php file:
$value = $_POST['phpEventId'];
echo "<div class = 'showNumber'>"."Nummer: ".$value."</div>";
It just says: Nummer:
It also gives no error whatsoever.
Thanks for your help!
Try
var eventID = $(this).attr('id');
Where this id comes from in your code ?
Passing it as JSON often gets the results I'm looking for. The server will interpret the JSON object as POST variables:
$.ajax({
url: "index.php",
type: "POST",
data: JSON.stringify({phpEventId: eventID}),
contentType: "application/json; charset=utf-8",
success: function (result) {
console.log(result);
}
});
The below code doesn't run in IE7/8. I researched online, .innerhtml will not work in IE 7/8 Browser. I really need this code to run in those browsers.
$(document).ready(function(){
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1);
$.ajax({
type: "GET",
url: "/get_header",
data: hashes,
success: function(data) {
if (!$.support.leadingWhitespace) {
alert('j');
document.getElementById('logo-bar').innerHTML = data;
} else {
$('#logo-bar').html(data);
}
},
error: function(data) {}
});
You're using jQuery for the AJAX request, so you may as well use it to update the DOM too. If you are concerned about leading whitespace in the response text, you can use $.trim() to remove it:
success: function(data) {
$('#logo-bar').html($.trim(data));
},
I have a javascript function that is called when the user clicks on a button and performs an AJAX query that adds some data to my database. However, I've been getting complaints that a lot of data hasn't been getting through, and I've isolated the problem to be the time between clicks. When they wait long enough between clicks, the data always gets through, but if they don't wait long enough it's a crapshoot.
So I'm pretty much settled that the problem is that the javascript function is being called again while it is already running, which I shouldn't allow. Is there a way I can lock the user's browser at the beginning of the function and unlock it at the end after the AJAX? I know this may irritate my users, but I can't see any other solution.
It's not totally necessary, but here's what my javascript function looks like:
function addtolist(thisform, sdata)
{
var scntDiv = $('#p_data');
var request = $.ajax({ async: false, url: "php_addtolist.php", type: "POST", data: {data:sdata}, dataType: "html" });
request.done(function(msg) { outdata = parseInt(msg); });
$(outdata).appendTo(scntDiv);
}
You can disable the button when the function is called, then re-enable it with the complete callback:
function addtolist(thisform, sdata)
{
$('#submit_btn').prop('disabled', true);
var scntDiv = $('#p_data');
var request = $.ajax({
async: false,
url: "php_addtolist.php",
type: "POST",
data: {data:sdata}, dataType: "html" },
complete: function() { $('#submit_btn').prop('disabled', false); }
);
request.done(function(msg) { outdata = parseInt(msg); });
$(outdata).appendTo(scntDiv);
}
Basically it seems like the outdata is async. the following should resolve.
function addtolist(thisform, sdata)
{
var scntDiv = $('#p_data');
var request = $.ajax({ async: false, url: "php_addtolist.php", type: "POST", data: {data:sdata}, dataType: "html" });
request.done(function(msg) {
outdata = parseInt(msg);
$(outdata).appendTo(scntDiv);
});
}
i am using ajax to get some data over to my page and use .html() to change the html content of a div.
Everything works fine in firefox , google chrome , safari , opera except INTERNET EXPLORER.
IE 7 , 8 , 9 are not responding to the .html() funciton, the contents of that div remains unchanged.
here's my code:
var userurl = $('#userthumb a').attr('href');
$(document).ready(function(){
$('#userthumb').after("<div id='to-change'>Loading...</div>");
$.ajax({
type: "GET",
url: "parse.php",
data: "url=" + userurl,
dataType: 'json',
cache: false,
success: function(data)
{
var respond = data['respond'];
$('#to-change').html(respond + 'profile');
} //end of success
}); //end of ajax
});
is there any problems or is there a way to solve the IE problem?
This might solve it:
success: function(data) {
eval('var jSON = '+data);
$('#to-change').html(jSON['respond'] + 'profile');
} //end of success
EDIT:
Make sure your returning data is in the format, for example:
{'respond':'it worked as expected','.....':'....'}
In my vbscripts I return:
response.write "{'Success':'MoveOn','....':'....'}" or
response.write "{'Success':'Error:........','....':'....'}"
Then,
eval('var jSON='+data);
if (jSON['Success'] == 'MoveOn') .......
Try
$('#to-change').html($.parseJSON(data).respond + 'profile');
Try this:
$('#to-change').empty().append(respond + 'profile');