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 ,)
Related
I am getting the following error message when clicking create button on a form. The create option is working fine on the RestClient.
"Child create error: error index.html:74:17" `in the web console. No error messages when running from Visual Studios:
// Called with "createForm" onSubmit
function createChild() {
$.ajax({
type: "POST",
contentType: "application/json",
url: rootURL,
dataType: "json",
data: formToJSONCreate(),
success: function (data, textStatus, jqXHR) {
console.log('Child created successfully');
clearCreateForm();
displayList();
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('Child create error: ' + textStatus + "" + errorThrown);
}
});
}
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
I came across jquery ajax status code online. But i am not sure what is the difference between this and getting status code through success and error. Any benefits of one over another?
// With statusCode
$.ajax({
url: 'abc.com/123/456',
type: 'get',
data: {},
statusCode: {
200: function(response){
},
0: function(response){
},
404: function(response){
}
},
success: function(data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
// withoutStatusCode
$.ajax({
url: 'abc.com/123/456',
type: 'get',
data: {},
success: function(data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
if (jqXHR.status === 0) {
} else if (jqXHR.status === 404) {
}
}
});
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'm trying to get JSON data from another domain.
It returns status 200 but invokes an error in the callback function.
Is it possible to get the raw data or html or even just text when I get into the error callback?
Why is xhr.responseText undefined?
Here is part of my code:
$.ajax({
type: 'POST',
url: 'http://timetable.nctu.edu.tw/?r=main/get_cos_list',
dataType: 'jsonp',
crossDomain: true,
async: false,
data:{m_acy:'**',m_sem:'**',m_degree:'**',m_dep_id:'**',m_group:'**',
m_grade:'**',m_class:'**',m_option:'cos_code',m_crsname:'**',m_teaname:'**',
m_cos_id:'**',m_cos_code:'DAM1346',m_crstime:'**'},
jsonp: true,
success: function(json) {
$('#jjjj').html(json);
},
error: function(xhr, textStatus, errorThrown) {
$('#content').append("readyState: "+xhr.readyState+"<br>status: "+xhr.status+"<br>responseText: "+xhr.responseText+"<br><hr>");
alert('Ajax request error.' + xhr.responseText + xhr.responseData + errorThrown);
},
complete: function(xhr, textStatus)
{
alert(xhr.responseText + xhr.responseHtml);
alert(h.responseHtml);
}
});