1.8.2
$.ajax({
url: "/someurl/",
async: true,
dataType: 'json'
}).done(function ( data ) {
if( console && console.log ) {
console.log("Sample of data: ", data);
}
});
causes error "Uncaught TypeError: Cannot call method 'done' of undefined", but request sends and server response comes to me with data!
If I wrote
$.ajax({
url: "/someurl/",
async: true,
dataType: 'json',
success: function (data) { console.log(data); }
});
That's ok and console.log fires!
It seems likely that your error probably comes from the way your using the response data, because your snippet is correct.
Why don't you run a fail method and test for errors there?
$.ajax({
url: "/someurl/",
async: true,
dataType: 'json'
}).fail(function (error) {
console.log(error);
});
Alternatively, check your Network Tab for xhr response preview?
Related
I have a function in ajax :
$.ajax({
type: "POST",
url: "#Url.Action("FiltraLevatamento", "Consulta")",
data: JSON.stringify(jsn),
contentType: "application/json",
dataType: "json",
async: true,
success: function (data) {
result(data);
$("#modalboxProcessa").modal("hide");
},
error: function (XMLHttpRequest, txtStatus, errorThrown) {
$("#modalboxProcessa").modal("hide");
$("#modalboxErro2").modal("show");
}
});
In my local machine works properly only when I publish in the server error in ajax .
Can anyone tell me what is the problem ? I have pretty much the same ajax only returning me other than data, and is running fine .
I have searched lot of on this issue and tried almost everything but it doesn't seem to be work so posting it here.
I am trying to make jquery ajax call to ashx handler by passing json data. But my request doesnt reach till handler GetBalance.ashx
code :
var mydata = {};
$.ajax({
url: 'Handlers/GetBalance.ashx?type=authenticateRequest',
type: 'post',
dataType: 'json',
cache: false,
data: mydata,
success: function (data) {
},
error: function (xhr, status) {
console.log(status);
console.log(xhr.responseText);
}
});
in console it prints
parsererror
(empty string)
What i am doing wrong ? It should reach till .ashx before it gives any response parse error
Edit:
Changed as suggested by answer
Replace your line
data: mydata,
with
data: JSON.stringify(mydata),
contentType: "application/json",
Here is the code I am using to access my web API controller named Owner; the success function is not being called. Any ideas?
$.ajax({
type: "GET",
url: 'http://localhost:26533/api/Owner',
contentType: "application/json",
dataType: "jsonp",
success: function (response) { alert("yes"); }
});
Remove the contentType and dataType and check the response..
Here an example:
$.ajax({
type: 'GET',
url: 'http://localhost:26533/api/Owner',
success: function(data){
alert(data);
},
error: function(xhr, type, exception) {
// if ajax fails display error alert
alert("ajax error response type " + type);
}
});
With this you can see what's wrong...
I'm trying to call a php script with a get request and using the data theaddress however the results are showing me the source of the page im calling.
The page im calling is here
Here is my ajax function that will get this page
$( document ).ready(function() {
var address = document.getElementById("address");
$.ajax({
url: '/r10database/checkSystem/ManorWPG.php',
type: 'GET',
data: 'theaddress='+address.value,
cache: false,
success: function(output)
{
alert('success, server says '+output);
}, error: function()
{
alert('Something went wrong, saving system failed');
}
});
});
$( document ).ready(function() {
var address = document.getElementById("address");
$.ajax({
url: '/r10database/checkSystem/ManorWPG.php',
type: 'GET',
data: 'theaddress='+address.value,
cache: false,
success: function(output)
{
alert('success, server says '+output);
}, error: function(error)
{
alert (error); // this is the change from the question
}
});
});
Put the dataType as json with a curly brace
data: {theaddress:address.value},
dataType:'json',
success: function(output)
{
alert('success, server says '+output);
}, error: function(xhr)
{
alert (xhr.status);
}
and get the data in ManorWPG.php as $_GET['theaddress']
** share the xhr.status if failed.
I have a php script, which return serialized in php data. And I try to receive this data by using $.ajax() method from jQuery 1.7. Here is the example.
$.ajax({
url: 'http://input.name/get.php?do=lookup' + '&domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
type: 'GET',
dataType: 'text',
cache: 'false',
complete: function(res) {
alert('COMPLETE() done');
console.log(res);
}
});
In console I see only
Object { readyState=0, status=0, statusText="error"}
So, what I do wrong? Could you help me please?
UPD
Interesting notice: if I use JSONP dataType request can receive data, but can't process it.
Here is an example.
$.ajax({
url: 'http://input.name/get.php?do=lookup',
data: 'domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
dataType: 'jsonp',
cache: false,
success: function(data) {
alert("Data: "+data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error: "+textStatus);
console.log(jqXHR);
}
});
Instead of complete: use success: then res will be the returned data from your ajax request.
Remember to use error: as well incase there is an error with you call, as it seems that there might be in your console output.
Code:
$.ajax({
url: 'http://input.name/get.php?do=lookup',
data: 'domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
cache: false,
success: function(data) {
alert("Data: "+data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error: "+textStatus);
console.log(jqXHR);
}
});
Your code is probably fine, but you're trying to violate the same origin policy. Basically, if your site is http://aaa.com/, you cannot make AJAX called to http://bbb.com/.
There are a few ways around it:
Getting around same origin policy in javascript without server side scripts
But most of them require that both sides play nice.
The response is the second parameter of complete function:
$.ajax({
url: 'http://input.name/get.php?do=lookup' + '&domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
type: 'GET',
dataType: 'text',
cache: 'false',
complete: function(res,response) {
alert('COMPLETE() done');
console.log(response);
}
});
More info: http://api.jquery.com/jQuery.ajax/
You should also consider using JSON, not php serialized data