How to take the header values in jquery [duplicate] - javascript

This question already has answers here:
jQuery - get AJAX response headers
(2 answers)
Closed 6 years ago.
Here i am calling one API, i am getting success response but they returning the total count header part , i don't know how to take the values.
See Here:
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function(result) {
console.log(result);
},
error: function(errMsg) {
//console.log(errMsg);
}
});

Try this: request.getResponseHeader('x-Total-Count') in success callback
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function(data, textStatus, request){
alert(request.getResponseHeader('x-Total-Count'));
},
error: function(errMsg) {
//console.log(errMsg);
}
});

the success callback has 3 params, you can Click here
$.ajax({
url: '//www.examples.com/api/get/',
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (result, textStatus, xhr) {
console.log(xhr.getResponseHeader('X-Total-Count'));
},
error: function(errMsg) {
//console.log(errMsg);
}
});

Related

Handle undefined data in ajax request

I have the below ajax call and sometimes the request stops the page from loading as the data being passed is undefined.
I there a way to put a condition to handle the request if it has values that are undefined?
Can it be wrapped with a if condition?
newuser is not defined
$.ajax(
{
url: 'sample.aspx,
data: newuser,
type: 'POST',
dataType: 'html',
success: function(data){
...
} ,
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
});
Simplest way would be to use a pipe:
$.ajax({url: 'sample.aspx',
data: newuser || {},//continue here...
If your variable was not initialized, empty object will be sent instead.
That's if and only if you can handle empty "newuser" for some reason.
I'm assuming that not closed URL is just a mistake in copy-paste, and not actually part of your code.
how about
if(newuser)
{
$.ajax(
{
url: 'sample.aspx,
data: newuser,
type: 'POST',
dataType: 'html',
success: function(data){
...
} ,
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
});
}
If you are unable to handle an empty newuser you can try something like this:
if (newuser) {
$.ajax({
url: 'sample.aspx',
data: newuser,
type: 'POST',
dataType: 'html',
success: function(data) {
...
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
}

Blank response from api

$.ajax({
type: "POST",
url: "http://api.xyz.com/go/login",
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
data:{id:'547',password:'password'},
success: function (data, status, jqXHR) {
alert("success");// write success in " "
},
error: function (jqXHR, status) {
// error handler
console.log(jqXHR);
alert('fail' + status.code);
}
});
This is the sample of code I used to get response.
what's wrong here?
The code isn't responding json file.

AJAX post is empty on the server side (PHP)

I am making an AJAX request to a PHP controller, by using jQuery ajax, but when trying to get the posted data with PHP the $_POST is empty. Below is the actual function:
function GetSeriesForManufacturer(manuf) {
selectedmanufacturer = manuf;
//Make an AJax Call For Getting Series Of Manufacturer
var series = null;
$.ajax({
type: "POST",
url: url,
data: "{manufacturer:'" + selectedmanufacturer + "'}",
contentType: "application/json", //; charset=utf-8",
dataType: "json",
cache: false,
async: false,
success: function (response) {
//remove loading gif
$(".loading").hide();
//Append Data
AppendSeries($.parseJSON(response.text), selectedmanufacturer);
//Custom Scrollbar Call
$('.MatchingSeries ul').mCustomScrollbar();
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }
});
}
Thanks in advance!
First, you don't need to stringify data. Just send object literal is ok.
data: {manufacturer: selectedmanufacturer},
Second, you don't need this line:
contentType: "application/json",
Let jQuery do the encoding for you:
$.ajax({
type: "POST",
url: url,
data: {
manufacturer: selectedmanufacturer
},
contentType: "application/json", //; charset=utf-8",
dataType: "json",
cache: false,
async: false,
success: function (response) {
...
},
error: function (XMLHttpRequest, textStatus, errorThrown) { }});

AJAX success call back not working

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...

How to write ajax get in jsp using jquery? [duplicate]

This question already has answers here:
jQuery XML error ' No 'Access-Control-Allow-Origin' header is present on the requested resource.'
(2 answers)
Closed 9 years ago.
I m try to write ajax get by using jquery in jsp file. But it always gave error message. I could`nt find the error in my method.
This is my ajax function. Please help me.
<script type="text/javascript">
$.ajax({
url: "http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list",
type: "GET",
data: '{"partyid":4}',
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function(msg){
alert("success : "+data);
},
error:function(msg){
alert("failure");
}
});
</script>
Error is :
XML HttpRequest cannot load http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list.
No 'Access-Control-Allow-Orgin' header is present on the requested resource. Orgin 'http:// localhost:4040' is therefore not allowed access
Try your data
data: {"partyid":4},
in place of
data: '{"partyid":4}',
And try your success callback like,
success: function(msg){
alert("success : "+msg);// use msg not data
},
Full Code
$(function(){
$.ajax({
url: "http://crowderia.cloudapp.net/ichainwsDev/api/rest/json/product.list",
type: "GET",
data: {"partyid":4},
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
success: function(msg){
alert("success : "+msg);
},
error:function(msg){
alert("failure");
}
});
});
You cloud app url works

Categories