Getting no response from an ajax call - javascript

I have been trying to get this ajax call to work for several hours and have not been able to resolve any possible issues as everything appears to be coded properly according to jQuery Ajax Call. From what I can tell the code is not even being executed. However when I manually run the API call I get the following JSON result:
<UnitsOfMeasure.UnitsOfMeasureDataWithMessage xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Corporate.Web.API.DAL">
<Message/>
<Success>true</Success>
<UnitOfMeasureBaseUnitItem i:nil="true"/>
<UnitOfMeasureBaseUnitList i:nil="true"/>
<UnitOfMeasureRelatedUnitItem i:nil="true"/>
<UnitOfMeasureRelatedUnitList i:nil="true"/>
<UnitOfMeasureSetItem i:nil="true"/>
<UnitOfMeasureSetList i:nil="true"/>
<UnitOfMeasureTypeItem i:nil="true"/>
<UnitOfMeasureTypeList i:nil="true"/>
<UsedForID>d64d99f9-acfa-44cd-a199-4557b7b20912</UsedForID>
</UnitsOfMeasure.UnitsOfMeasureDataWithMessage>
My API Code is as follows:
[Route("api/admin/UnitsOfMeasure/UnitOfMeasureDefaultUsedForGetID")]
[HttpGet]
[ResponseType(typeof(UnitsOfMeasure.UnitsOfMeasureDataWithMessage))]
public IHttpActionResult UnitOfMeasureDefaultUsedForGetID(string UnitOfMeasureDefaultUsedForName)
{
UnitsOfMeasure _oUnitsOfMeasure = new UnitsOfMeasure();
return Ok(_oUnitsOfMeasure.UnitsOfMeasureSet_GetUsedForID(UnitOfMeasureDefaultUsedForName));
}
My DAL is supposed to return a Boolean flag and a Guid.
And my Ajax call is as follows:
$.ajax({
type: "GET",
url: g_WebServiceUnitOfMeasureDefaultUsedForGetIDURL,
data: {"UnitOfMeasureDefaultUsedForName": "Purchase"},
datatype: "json",
success: function(jsonResult){
DefaultUnitUsedForID = jsonResult;
},
failure: function (xhr, textStatus, errorThrown) {
console.log(xhr);
console.log("---------------------------------------------------------------------------");
console.log(textStatus);
console.log("---------------------------------------------------------------------------");
console.log(errorThrown);
console.log("---------------------------------------------------------------------------");
},
error: function (xhr, textStatus, errorThrown) {
console.log(xhr);
console.log("---------------------------------------------------------------------------");
console.log(textStatus);
console.log("---------------------------------------------------------------------------");
console.log(errorThrown);
console.log("---------------------------------------------------------------------------");
}});
As mentioned, from stepping through the code, it does not even enter the success or failure functions. What am I doing wrong?

May be it is foolish, But give it a shoot
$.get( g_WebServiceUnitOfMeasureDefaultUsedForGetIDURL,
{"UnitOfMeasureDefaultUsedForName": "Purchase"} )
.done(function( data ) {
DefaultUnitUsedForID = jsonResult;
alert( "Data Loaded: " + data );
});

Related

Handle data from ajax request after call

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);
}
});
});

ReferenceError: onclick function is not defined

I've gotten a "ReferenceError: function is not defined on the saveRoute function". Everything seems right to me but i could not figure out the problem.
Here is the related codes.
<script type="text/javascript">
var array = routeobj;
function saveRoute(){
var json = JSON.stringify(array)
$.ajax({
url : "http://192.168.1.9:11123/runornot/drawtrack",
type: "POST",
dataType:'json'
data : {json:json}
{"routeJson": json },
console.log("hellohelloworldolr");
success: function(data, textStatus, jqXHR)
{ //data - response from server
console.log("checkin success"); },
error: function (jqXHR, textStatus, errorThrown)
{
}});}
</script>
and in the html
Save
<script type="text/javascript">
var array = routeobj;
function saveRoute(){
var json = JSON.stringify(array) // <--- best practice would require a semicolon here
$.ajax({
url : "http://192.168.1.9:11123/runornot/drawtrack",
type: "POST",
dataType:'json' // <--- missing a comma
// the structure doesn't make any sense starting here...
data : {json:json} // <--- should be a comma here?
// missing property name?
{"routeJson": json },
// is this supposed to be the body of a function?
console.log("hellohelloworldolr");
// things start making sense again here...
success: function(data, textStatus, jqXHR)
{ //data - response from server
console.log("checkin success"); },
error: function (jqXHR, textStatus, errorThrown)
{
}});}
</script>

Internal Server Error when response text from ajax POST call is big

I am using an ajax call like the following:
$(function () {
$.ajax({
type: 'POST',
url: 'frmHistoryReportFinal.aspx/GetDataTable',
data: json_data,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (responseText) {
if (responseText.d == "")
return;
parsedData = JSON.parse(responseText.d);
alert(parsedData);
count = 0;
},
error: function (xhr, textStatus, errorThrown) {
//process the error
alert(xhr);
alert(textStatus);
alert(errorThrown);
}
});
});
When I have a small set of data returned from my C# codebehind. The ajax call functions well. But when I have a json string with larger data, say 200 records returned then the ajax call gives error("Internal Server Error").
Please help me resolve the issue as I usually need to handle large datasets.

Check is a Self-Hosted-Service is running or not

I have a Self-Hosted-Service(using WCF) that will run on clients machines. That service is supposed to make request to another server, get the data as XML then it returns to me that data as JSONP. Now i want to check if the service is running or not .. How can i check that ?
In my JS code i use $.getJSON with callback, so i tried to use .fail like this:
$.getJSON("http://localhost:8080/url?callback=?", function () {
alert("success");
}).fail(function () {
alert('fail');
})
but fail function didn't called when the server is not running(on chrome the Type is pending and Status is Failed)
Then i tried to use $.AJAX like this:
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://localhost:8080/url?callback=?',
success: function (data, textStatus) {
alert('request successful');
},
error: function (xhr, textStatus, errorThrown) {
alert('request failed');
}
});
I got the same result.
When you make the AJAX request to your localhost and /url? returns weather the other server is up or not, your script won't fail. Because http://localhost/url is online.
I'd make the /url script return JSON array with remoteHostOnline: true or false,
then use:
$.ajax({
type: 'GET',
dataType: 'jsonp',
url: 'http://localhost:8080/url?callback=?',
success: function (data, textStatus) {
if (data.remoteHostOnline == false) {
alert('remote host not online');
}
}
});
You might have to tweak this script I didn't test it but you will understand what's wrong.

AJAX call with jQuery + Coffeescript not generated correctly

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 ,)

Categories

Resources