Opposite of success: function(data) - ajax - ajax

what is the opposite of -> success: function(data)
$.ajax({
type: "POST",
cache: false,
url: "/p.php",
data: info,
success: function(data){
I want to check if it is not success instead of if it is success...

You catch success situation with success function and there is also error function. Please see the documentation http://api.jquery.com/jquery.ajax/
And please see this for detailed xhr catch mechanizm
$.ajax({
type: "POST",
cache: false,
url: "/p.php",
data: info,
success: function(data){
//do something
},
error: function(jqXHR, textStatus, errorThrown){
console.log(errorThrown);
}

Related

laravel and sweetalert and rederect to

i use laravel 5.8 and i have a a problem if i insert or update a page than i go not back to the page list.
postcontroller :
i use : return response()->json('Post Created');
and i use a ajax file whit this code :
$.ajax({
type: 'POST',
url: this.action,
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
success: function(response){
Sweet('success',response)
success(response)
},
error: function(xhr, status, error)
{
$('.errorarea').show();
$.each(xhr.responseJSON.errors, function (key, item)
{
Sweet('error',item)
$("#errors").html("<li class='text-danger'>"+item+"</li>")
});
errosresponse(xhr, status, error);
}
})
});
i get a nice message if it is insert or updated but it not go back to my list, hope you understand what i mean.
if you want to redirect after success do something like this:
$.ajax({
type: 'POST',
url: this.action,
data: new FormData(this),
dataType: 'json',
contentType: false,
cache: false,
processData:false,
success: function(response){
Sweet('success',response)
success(response)
window.location.href = "url you want to redirect to";
},
error: function(xhr, status, error)
{
$('.errorarea').show();
$.each(xhr.responseJSON.errors, function (key, item)
{
Sweet('error',item)
$("#errors").html("<li class='text-danger'>"+item+"</li>")
});
errosresponse(xhr, status, error);
}
})
});

the alert inside my success function response on ajax wont appear?

$.ajax({
type: "POST",
url: url,
data: data,
success: function(response){
alert("success"); --> this is the one
}
});
I've successfully inserted the data from my sign-up form i just want to inform the user that they successfully registered on the alert box
Maybe it's not succeeding. Have you tried throwing in an error handler to see if it's failing?
$.ajax({
type: "POST",
url: url,
data: data,
success: function(response){
alert("success"); --> this is the one
},
error: function (XHR, status, error){
console.log('error', error);
}
});

Cross-origin ajax not working

I am having a problem while using cross-origin ajax.
I know it's a common question but did not get any solution for it yet.
$.ajax({
type: "GET",
url: url,
data: {id:id},
dataType: "jsonp",
crossDomain: true,
contentType: "application/jsonp; charset=utf-8",
async: false,
success: fnsuccesscallbackk,
error: function(xhr, error){
alert(error);
},
jsonpCallback: fnsuccesscallback
});
function fnsuccesscallback(data){
alert(data)
}
…but getting undefined response in callback function.
Is there anything wrong what I am doing.
After a lots of RND finally i got the solution of this.
Ajax function:
$.ajax({
type:"GET",
url:'https://www.url.com/welcome/test_js',
data:{name:'xyz'},
crossDomain:true,
dataType: "jsonp",
jsonp: 'fnsuccesscallback',
success: function(data) {
alert(data.name)
}
});
In the Php function:
function test_js() {
echo $_GET['fnsuccesscallback'] . "(" . json_encode($_GET) . ")";
}

How can I get response header using AJAX

I'm trying to get response header using AJAX, but it doesn't work. Apparently I did't do it right:
$.ajax({
type: 'GET',
url:'http://www.somesite.com/',
data: formData,
success: function(data, textStatus, XMLHttpRequest){
alert(XMLHttpRequest.getResponseHeader('some_header'));
}
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.getResponseHeader('some_header'));
}
});
I also try this one:
$.ajax({
type: "GET",
url: 'http://www.somesite.com/',
complete: function(xhr) {
alert(xhr.getAllResponseHeaders());
}
});
Any help?
You are missing a comma.
success: function(data, textStatus, XMLHttpRequest){
alert(XMLHttpRequest.getResponseHeader('some_header'));
}, // <-------------

jQuery ajax POST request with application/json

I want to make a POST request to the remote server from jQuery. When I write code like this
$.ajax({
type: 'POST',
url: 'http://mysite:8080/orderService/order/send',
crossDomain: true,
data: JSON.stringify(orderSendRequest),
dataType: 'json',
success: function(responseData, textStatus, jqXHR) {
var value = responseData.someKey;
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
Everything is ok, but I want ContextType to be application/json, and when I add this line to the code the request doesn't work and I have the following error:
XMLHttpRequest cannot load http://mysite:8080/orderService/order/send. Origin null is not allowed by Access-Control-Allow-Origin.
$.ajax({
type: 'POST',
url: 'http://mysite:8080/orderService/order/send',
crossDomain: true,
data: JSON.stringify(orderSendRequest),
dataType: 'json',
contentType : 'application/json; charset=utf-8',
success: function(responseData, textStatus, jqXHR) {
var value = responseData.someKey;
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
I don't believe json supports crossDomain. Research using jsonp datatype instead.

Resources