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 = [{}];
Related
Here is my javascript codes :
$.ajax({
url: "/Users_Area/Order.aspx/Get_Server_Date",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: "{}",
async: true,
success: function(result) {
server_date = new Date(result.d);
//alert(server_date);
var Order_id = $("#<%= hf_order_id.ClientID %>").val();
alert(Order_id);
$.ajax({
url: "/Users_Area/Order.aspx/Get_Order_Date",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: "{'Order_id': '" + Order_id + "'}",
async: true,
success: function(result) {
order_date = new Date(result.d);
alert(order_date);
if (server_date > order_date) {
var dif_server_order_in_seconds = (server_date - order_date) / 1000;
alert(dif_server_order_in_seconds);
if (dif_server_order_in_seconds > 3600) {
} else {}
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
debugger;
}
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
debugger;
}
});
And here is console error after alert(order_date); line :
Browser Link: Failed to send message to browser link server: Error:
SignalR: Connection has not been fully initialized. Use
.start().done() or .start().fail() to run logic after the connection
has started.
How can i fix this problem?
(I am using asp.net webforms with c#)
I want to use POST method with AJAX in SAPUI5 javascript but I found an error.
var url = "https://xxxx*xxxx.co.id:8877/TaspenSAP/SimpanDosirPunah";
$.ajax({
type: "POST",
url: url,
data: JSON.stringify({
nomorDosir: "01001961288",
kodeCabang: "A02"
}),
dataType: "json",
async: false,
contentType: 'application/json; charset=utf-8',
success: function(data, textStatus, xhr){
console.log("sukses: " + data + " " + JSON.stringify(xhr));
},
error: function (e,xhr,textStatus,err,data) {
console.log(e);
console.log(xhr);
console.log(textStatus);
console.log(err);
}
});
error:
I already did change code with dataType=text, or data: {nomorDosir: "01001961288", kodeCabang: "A02"} (without stringify), but I not yet find the solution. How to fix this problem?
Thanks.
Bobby
Not sure what your use case is but if you are trying to post to an oData service, it might be much easier to use SAPs createEntry method where the URL is the path to the model you want to post to and your JSON are the properties:
var oModel = new sap.ui.model.odata.v2.ODataModel("https://services.odata.org/V2/OData/OData.svc/");
//oModel should use your service uri
var url = "https://xxxx*xxxx.co.id:8877/TaspenSAP/SimpanDosirPunah";
oModel.createEntry(url, {
properties: {
nomorDosir: "01001961288",
kodeCabang: "A02"
}
}, {
method: "POST",
success: function(response) {
alert(JSON.stringify(response));
//do something
},
error: function(error) {
alert(JSON.stringify(error));
}
});
oModel.submitChanges();
What you have is wrong json format, you have:
data: JSON.stringify({nomorDosir: "01001961288", kodeCabang: "A02"}),
Which actually should be:
data: {"nomorDosir": "01001961288", "kodeCabang": "A02"},
Which then you don't need to do a json.stringify on, because it already IS a json format. Hope this will help you out.
Which you could also try is setting a variable outside like this:
var url = "https://xxxx*xxxx.co.id:8877/TaspenSAP/SimpanDosirPunah";
var json = {"nomorDosir": "01001961288", "kodeCabang": "A02"};
$.ajax({
type: "POST",
url: url,
data: json,
dataType: "json",
async: false,
contentType: 'application/json; charset=utf-8',
success: function(data, textStatus, xhr){
console.log("sukses: "+data+" "+JSON.stringify(xhr));
},
error: function (e,xhr,textStatus,err,data) {
console.log(e);
console.log(xhr);
console.log(textStatus);
console.log(err);
}
});
function GET() {
jQuery.support.cors = true;
$.ajax({
url: 'http://localhost:32253/api/UserDetail/GetRoleDetails',
type: 'GET',
async:true,
contentType: "application/json",
dataType: 'json',
xhrFields: {
withCredentials: true
},
error: function (xhr, status)
{
alert(status);
},
success: function (data) {
alert("Success!");
}
});
return false;
}
I am very much new to ajax,I tried a ajax call to my services method which returns value.But the success callback function is never fired .I only get error.What might be the reason?
I tried using dataType to jsonp and other similar solutions which had been found in Stackoverflow regarding this issue.Nothing worked out.I am getting server response as 200 oK.
try this code
$.ajax({
type: "POST",
url: URL,
async: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
jsonp: "onJSONPLoad",
jsonpCallback: "newarticlescallback",
crossDomain: "false",
beforeSend: function (xhr) {
},
error: function (request, status, error) {
console.log("Error In Web Service...." + request.responseText);
return false;
},
success: ajax_success,
complete: function (xhr, status) {
}
});
create function "ajax_success" in ur page
like below
function ajax_success(parsedJSON) { //you code here
}
this may be help you
try this code
$.ajax({
type: "POST",
url: URL,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
jsonp: "onJSONPLoad",
jsonpCallback: "newarticlescallback",
crossDomain: "false",
beforeSend: function (xhr) {
},
error: function (request, status, error) {
console.log("Error In Web Service...." + request.responseText);
return false;
},
success: function (data) {
Result = data;
},
complete: function (xhr, status) {
}
});
I'm trying to capture data from an HTML page that is on the another website. I need to capture that data and save it into my site. That's why I used cross domain ajax like this
var myCallback = function(data) {
console.log(data);
};
var formData = $('.data-capture-form').serialize();
$.ajax({
url: 'http://prospectbank.co.uk/leads/capt',
type: 'GET',
data: formData,
dataType: 'jsonp',
crossDomain: true,
jsonp: 'callback',
jsonpCallback: 'myCallback'
}).done(function(res) {
console.log(res);
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
});
Then I get this error
Any help? Thanks
The response from http://prospectbank.co.uk/leads/capt?_=1340513330866? is wrong formatted JSON.
{"status":true}
It should be
{status:true}
below ajax call throws error message. I can't figure out what is wrong. Thanks
$('#filterform input:text[name=other_location]').bind('keyup', function() {
var val = $('#filterform input:text[name=other_location]').val();
$.ajax({
url: 'http://linkedin.com/ta/region?query='+val,
//contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
alert('success');
},//success
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("XMLHttpRequest="+XMLHttpRequest.responseText +"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
}
});
});
What you need is JSONP
dataType: "jsonp"