I have an application which is trying to sent an ajax request to an external api, the request returns with a connection refused error on console. Below is the code I am using along with the image of the actual issue. Any suggestions on this ? or if someone can point me in the right direction, thanks.
$.ajax({
url: url,
type: "POST",
data: args,
timeout: 15000,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (err) {
console.dir(err);
}
});
Console error image
Related
I have a js when i do a ajax call to one server. When i call to this service using localhost, works. But when i call to this service using the server where i upload it, its ERROR 500. The call return a json.
$.ajax({
url: "https://www.example.com/example",
dataType: "json",
data: {
'data': xml,
'message': message,
'customer_id': customer_id,
'subscr_id': subscr_id
},
type: 'POST',
success: function (devol) {
},
error: function (xhr, ajaxOptions, thrownError) {
alert("no ha entrado");
}
});
Try to update your code below:
var model = {
'data': xml,
'message': message,
'customer_id': customer_id,
'subscr_id': subscr_id
};
$.ajax({
url: "https://www.example.com/example",
type: 'POST',
dataType: 'json',
data: JSON.stringify(model),
//headers: { 'authorization': `Bearer ${token}` },
async: false,
processData: false,
contentType: "application/json",
error: function (err) {
},
success: function (data) {
}
});
It might be because of CORS (Cross-Origin Resource Sharing)... Usually, you cannot make calls to other domains from the browser unless the other domain you are making calls allows CORS to all sites or specific sites.
enter link description here
Return error in the query
From the browser the answer is correct.
$.ajax({
type: "POST",
url: url,
async: true,
contentType: " charset=utf-8",
dataType: "XMLHttpRequest",
success: function (response) {
console.log(response);
},
error: function (msg) {
console.log(msg);
}
});
The message says "error".
I see three issues. First, dataType is a choice of xml, json, script, or html, unless you did something really fancy. jQuery can guess it based on received data though, so there is normally no need to set it. But if you want to be explicit (assuming your page returns json):
dataType: "json"
Second, contentType value looks like some truncated thing. I would just completely remove it, as you are not sending any data and just requesting a page.
Finally, when you are sending no data and just requesting a resource, the best is to use GET.
All in all:
$.ajax({
type: "GET",
url: url,
async: true,
dataType: "html",
success: function (response) {
console.log(response);
},
error: function (msg) {
console.log(msg);
}
});
I am using cordova and while doing json i am getting an error "Failed to load resource: the server responded with a status of 400 (Bad Request)".
But the same code when I run it on postman is getting the right answer.Please help me to solve this problem.
The code is:
$.ajax({
url: url,
type: "POST",
async: false,
ContentType: "application/json; charset=utf-8",
data: jData,
dataType: "json",
success: function(response) {
console.log(response)
},
error: function(jqXHR, textStatus, errorThrown) {
},
});
And a screenshot of the right answer on the postman is also given for your reference
you need to stringify the JSON data was sending
$.ajax({
type: 'POST',
url: url,
async: false,
data: JSON.stringify(jData),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(response) {
console.log(response)
},
error: function(jqXHR, textStatus, errorThrown) {
}
});
Try removing the open and closing brackets around your jData
var jData = {};
Not
var jData = [{}];
I have hosted a service sucessfully on IIS.
Made sure that application is running.
I have given call to this service through ajax as:
var parameters = {
'EmailID':EmailID,
'Password':Password
};
$.ajax({
url: "http://localhost:85/MobileECommerceTesting/Service1.svc/validateLogin",
data: JSON.stringify(parameters),
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST",
cache: false,
success: function (Data) {
alert("asdsad");
},
error: function () {
alert("Error in Saving.Please try later.");
}
});
But its not giving call to the service.
What can be the problem?
EDIT:
EDIT2 :
Network Tab:
Take a look at this question:
the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
It might be related to your binding, which does not expect the content type application/json. You must use webHttpBinding to create "REST-like" services with WCF.
No matter what I try, this Ajax request doesnt seem to be going inside the success block. Can anyone tell me whats happening? This is driving me crazy.
$.ajax({
url: "/Index/AddItem",
type: "post",
contentType: "application/json",
data: JSON.stringify({
Srl: $("#Srl").val(),
Description: $("#Description").val(),
Comment: $("#Comment").val()
}),
headers: {
"RequestVerificationToken": "#TokenHeaderValue()"
},
dataType: "json",
success: function (data) {
alert("Done");
$('#tknGen').html(data);
}
});
Im trying to replace a with the response data. But even the alert is not getting fired. So, Im guessing its a problem with the success block
Try this
$.ajax({
url: "/Index/AddItem",
type: "post",
contentType: "application/json",
data: {
Srl: $("#Srl").val(),
Description: $("#Description").val(),
Comment: $("#Comment").val()
},
headers: {
"RequestVerificationToken": "#TokenHeaderValue()"
},
dataType: "json"
}).done(function (data) {
alert("Done");
$('#tknGen').html(data);
}).fail(function(jqXHR, textStatus, errorThrown) {
console.error(errorThrown);
});
"done" and "fail" callback are the recommended way to handle result since Query 1.8 (at replacement for success and error).
The fail callback will show up more information about the error thrown server-side.