Hi this code is working great, i am new in javascript ,what i am trying to do , print js default error message in my div.
<script type="text/javascript">
$(document).ready(function () {
var contranumber = <?php echo json_encode($data); ?>;
debugger;
if(contranumber)
{
$.ajax({
url: ApiUrl+'ActivateUser?contraNumber='+contranumber,
type: 'get',
success: function (data, xhr) {
alert(data.ErrorMessage);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
}else
{
}
});
</script>
<div><center>javascript message</center></div>
<center><h3> Check your Email</h3></center>
A few quick things first:
Remove center tags, as it is deprecated. (Official example is "No, really, don't use it.")
Remove debugger;, unless you want your browser to stop.
Give your elements some sort of identification, be it a class or ID.
$(document).ready(function() {
var contranumber = '{ "hello": "world" }';
var message = $('.message'); // the element the message will go in
if (contranumber) {
$.ajax({
url: ApiUrl + 'ActivateUser?contraNumber=' + contranumber,
type: 'get',
success: function(data, textStatus, xhr) { // you missed the textStatus parameter
message.text('it was successful'); // why log an error on success?
},
error: function(xhr, textStatus, errorThrown) {
message.text('Error in Operation:' + errorThrown); // give the actual error text
}
});
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="message"></div>
<h3>Check your Email</h3>
$('#example center').text(data.ErrorMessage);
I usually do this:
$.ajax({
type: 'POST',
url: validateAjaxURL,
success: function (data) {
var returnData = data;
if (returnData.match("^selectedUno-")) {
$('#new_caregiver_popup_div').dialog('close');
} else {
$("#new_caregiver_popup_div").html(data);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + XMLHttpRequest.responseText);
}
});
UPDATE :
error: function (XMLHttpRequest, textStatus, errorThrown) {
$('#DivID').text(errorThrown);
}
Here is the link for your reference :
Show Error
Related
i am trying to send datas from ajax to php, but php doesnt show it. both scripts are on the same site: "testpage.php".
jQuery/Ajax:
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$("#button").click(function() {
var test = "bla";
$.ajax({
url: "testpage.php",
type: "post",
data: test,
success: function (response) {
alert("test ok");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
});
</script>
PHP:
<?php
if(isset($_POST["test"])) {
$test2 = $_POST;
echo $test2;
echo "Test";
}
?>
I do not see the result of PHP
use data: {test:test}, & alert your response to see your result.
success: function (response) {
alert(response);
},
Or Just append your response to html.
You need to use data: {test:sometext} if you want to do a POST request.
The PHP can't see the value in the post because you only send bla in the PHP body. You have to send test=bla. But jQuery can do it automatically by sending data : { test : test }.
$(document).ready(function() {
$("#button").click(function() {
var test = "bla";
$.ajax({
url: "testpage.php",
type: "post",
data: {
test : test
},
success: function (response) {
alert("test ok");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
});
You can use serialize() method into your $.ajax();
Something like:
$.ajax({
url: "testpage.php",
type: "post",
data: $("#myForm input").serialize(),
success: function (response) {
alert("test ok");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
Then try to accessing your post data by form input name.
var data = {
'test': "bla",
};
$.ajax({
type: "POST",
url: "testpage.php",
data: data,
dataType: "json"
}).done(function(response){
console.log(response);
}).fail(function(response){
console.log(response);
});
I'm using ajax to get the returned value from php function, the call is correct but I can't access the data properly.
The ajax call is:
$.ajax({
data: {"hotel_id" : hotel_id},
url: '/get_type_check',
type: 'get',
success: function (response) {
console.log(response);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
If I print the console log shows:
<!DOCTYPE html>
comment:
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
{"status":["CAB2"]}
And the php function:
public function get_type_check(){
$type_checks=Hotel::get_type_checks($_GET['hotel_id']);
echo json_encode(array('status' => $type_checks));
}
How can I get the response.status?
Should I use return instead of "echo"?
You have to parse the response to json to catch it as json.
Just add the line:
var data = $.parseJSON(response);
So your ajax will as follows:
$.ajax({
data: {"hotel_id": hotel_id},
url: 'ajax.php',
type: 'get',
success: function(response) {
var data = $.parseJSON(response);
console.log(data.status);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus);
alert("Error: " + errorThrown);
}
});
});
I have the following Coffeescript:
$(document).ready ->`
$.ajax 'http://www.omdbapi.com/?i=tt1285016',
type: 'GET'
dataType: 'json'
error: (jqXHR, textStatus, errorThrown) -> $('body').append "AJAX Error: #{textStatus}"
success: (data, textStatus, jqXHR) -> $('body').append "Successful AJAX call: #{data}"
However this genrates the following Javascript which doesn't look right to me:
(function() {
$(document).ready(function() {
return $.ajax('http://www.omdbapi.com/?i=tt1285016');
});
({
type: 'GET',
dataType: 'html',
error: function(jqXHR, textStatus, errorThrown) {
return $('body').append("AJAX Error: " + textStatus);
},
success: function(data, textStatus, jqXHR) {
return $('body').append("Successful AJAX call: " + data);
}
});
}).call(this);
Can anyone tell me where this is going wrong?
Thanks,
Adam
I couldn't compile your current code (CoffeeScript version 1.6.1), but if you remove the ` symbol after $(document).ready -> it compiles to
(function() {
$(document).ready(function() {
return $.ajax('http://www.omdbapi.com/?i=tt1285016', {
type: 'GET',
dataType: 'json',
error: function(jqXHR, textStatus, errorThrown) {
return $('body').append("AJAX Error: " + textStatus);
},
success: function(data, textStatus, jqXHR) {
return $('body').append("Successful AJAX call: " + data);
}
});
});
}).call(this);
I'm not sure how CoffeeScript handles line breaks, but I guess you're compiling a file with Windows end-of-lines (CRLF) on a *nix system and that causes the problem.
I'm saying that because something like the following code:
$(document).ready ->
$.ajax 'http://www.omdbapi.com/?i=tt1285016',
type: 'GET'
dataType: 'html'
error: (jqXHR, textStatus, errorThrown) -> $('body').append "AJAX Error: #{textStatus}"
success: (data, textStatus, jqXHR) -> $('body').append "Successful AJAX call: #{data}"
compiles to the one you've posted. (note that there is a line-break on line 3 after ,)
I am doing the following through jQuery, but it looks like they are almost happening simultaneously.
Is there a way to make sure itemNameContainer.removeClass('NoDisplay') in the complete callback to run only after the loadingContainer.addClass('NoDisplay') is completed?
Visually, it looks like I am seeing 'Please wait..' and the item name show up at the same time..
function onToggleItemCompletionStatus(currentItem) {
var itemId, toggle = !currentItem.Completed,
loadingContainer, itemNameContainer;
itemId = currentItem.ItemId;
loadingContainer = $('#loading_' + itemId);
itemNameContainer = $('#name_' + itemId);
$.ajax({
beforeSend: function (xhr, settings) {
loadingContainer.removeClass('noDisplay');
itemNameContainer.addClass('noDisplay');
},
complete: function (xhr, textStatus) {
loadingContainer.addClass('noDisplay');
itemNameContainer.removeClass('noDisplay');
},
data: {
accessToken: aToken,
listId: currentGroceryList.Id,
itemId: currentItem.ItemId,
completed: toggle
},
dateType: 'json',
error: function (xhr, textStatus, errorThrown) {
$.publish(customEvent.ItemToggledFail, [currentItem]);
},
success: function (data, textStatus, xhr) {
var success = data.success;
if (success) {
$.publish(customEvent.ItemToggledSuccess, [currentItem]);
} else {
$.publish(customEvent.ItemToggledFail, [currentItem]);
}
},
type: 'POST',
url: actionUrls.toggleItemCompletionStatus
});
}
EDIT
I pasted the actual function to give a better idea
Not that I know of, but you could try...
loadContainer.fadeOut(300, function() {
itemNameContainer.removeClass('NoDisplay');
});
I have the following jQuery which does not give the most descriptive error messsages...
url: 'server_page.aspx',
type: 'POST',
data: { intID:$(this).attr("id"), strState:"1" },
error: function() { alert('Error'); },
success: function() { }
How do I get more descriptive error messages if it is possible?
EDIT:
This is the full javascript:
$(document).ready(function(){
$("input:checkbox").change(function() {
var that = this;
if($(this).is(":checked")) {
$.ajax({
url: 'favorite_on_off.aspx',
type: 'POST',
data: { strFavoriteID:$(that).attr("id"), strState:"1" },
timeout: 1000,
error: function(xhr, status, error)
{
alert("values: strFavoriteID: " + $(that).attr("id") + " strState: " + "1");
alert('Error: ' + status + '\nError Text: ' + error);
},
success: function() { }
});
} else {
$.ajax({
url: 'favorite_on_off.aspx',
type: 'POST',
data: { strFavoriteID:$(that).attr("id"), strState:"0" },
timeout: 1000,
error: function(xhr, status, error)
{
alert("values: strFavoriteID: " + $(that).attr("id") + " strState: " + "0");
alert('Error: ' + status + '\nError Text: ' + error);
},
success: function() { }
});
}
});
});
These are the error messages:
values: strFavoriteID: c:\folder\document.doc strState: 1
Error: error
Error Text: undefined
You can use all of the arguments passed to the error callback, for example:
error: function(xhr, status, error) {
alert('Error: ' + status + '\nError Text: ' + error);
},
The second argument provided to the error callback is textStatus, which should contain a description of the error:
error: function(xhr, textStatus) { alert(textStatus); }
Note that you should probably not provide this information to your users. Parse the message using Javascript and give them a nice friendly message explaining the error.
I have this method:
function HandleAjaxError(request, status, error) {
var ex = eval("(" + request.responseText + ")");
$('body').addClass("ui-widget-overlay");
alert(ex.Message);
$('body').removeClass("ui-widget-overlay");
}
$.ajax({
type: "POST",
url: window.location.pathname + "/DoStuff",
data: "{}",
success: Success,
error: HandleAjaxError
});
This way, I can gracefully handle the error message (and possibly the status code) on the ASP.NET side. I usually log the original error, then throw a custom/descriptive one for the client.