given this very simple ajax post using jquery
function addEntity(parent, entity, successCallback, errorCallback ) {
console.log("add entity:", JSON.stringify(entity));
$.ajax( '/service/v3/rest/' + parent.id, {
type: 'POST',
dataType: 'application/json;',
contentType:"application/json; charset=utf-8",
data: JSON.stringify(entity),
success: successCallback,
error:errorCallback
});
}
note the console log that is identical to that used in the data field:
console.log("add entity:", JSON.stringify(entity));
outputs exactly what i'd expect the post body to contain:
add entity: `{"name":"some name","execute":false,"listeners":{"foo":"bar"}}`
That's exactly correct for what i'm trying to do.
What is actually posted (seen through Charles Proxy) :
{
"name": "some name",
"listeners": {}
}
Why would jquery remove the contents of my map?
this magically made my post contain the map :/
function addEntity(parent, entity, successCallback, errorCallback ) {
console.log("add entity:", JSON.stringify(entity));
$.ajax( '/service/v3/rest/' + parent.id, {
type: 'POST',
dataType: 'json',
contentType:"application/json; charset=utf-8",
processData: false,
data: JSON.stringify(entity),
success: successCallback,
error:errorCallback
});
}
Related
I'm using the following code in my chat bot (using v4 azure MS bot framework), to query the question and answers (Client side code - using plain JavaScript and J Query),
function generateAnswer()
{
var question = {
question: "will you marry me"
}
$.ajax({
type: "POST",
url: "https://YourEndPointURL/qnamaker/knowledgebases/eb895acb-e034-4f7c-asda7c-1955458ecec6/generateAnswer&$filter=source eq 'Editorial'",
data: JSON.stringify(question),
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization','EndpointKey c44444_Your_Endpoint_Key_4556');
},
dataType: "json",
contentType: "application/json",
success: function (data) {
console.log(data);
console.log(data.answers[0].answer);
}
});
}
while using this code, i"m getting the following error response
The resource you are looking for has been removed, had its name
changed, or is temporarily unavailable.
So please help me with the correct syntax to apply filter for my query.
According to https://learn.microsoft.com/en-us/azure/cognitive-services/qnamaker/how-to/metadata-generateanswer-usage, you need to specify filters in the body (the data property)
function generateAnswer()
{
var data = {
question: "will you marry me",
strictFilters: [
{
"name": "source",
"value": "Editorial"
}],
}
$.ajax({
type: "POST",
url: "https://YourEndPointURL/qnamaker/knowledgebases/eb895acb-e034-4f7c-asda7c-1955458ecec6/generateAnswer",
data: JSON.stringify(data),
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization','EndpointKey c44444_Your_Endpoint_Key_4556');
},
dataType: "json",
contentType: "application/json",
success: function (data) {
console.log(data);
console.log(data.answers[0].answer);
}
});
}
Moreover, you are missing 2 things:
your hostname, to replace YourEndPointURL
endpoint key, to replace c44444_Your_Endpoint_Key_4556
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);
}
});
I have a XML object retrieved from an AJAX call and for which I have done some manipulations:
$.ajax({
url: "url_of_xml",
type: 'GET',
dataType: 'xml',
success: function (xml) {
var sld_doc= $(xml)
// manipulations with the XML file
}
})
The XML file is correctly modified and is how I need it to be (with the added/modified nodes). Now I need to POST the modified XML (to a GeoServer instance):
$.ajax({
url: "geoserver/rest/styles",
type: 'POST',
data: sld_doc,
headers: {
"Content-Type": "application/vnd.ogc.sld+xml"
},
dataType: 'json',
success: function (data) {a
console.log(JSON.stringify(data));
},
error: function (x, e) {
console.log(x.status + " " + x.responseText);
}
});
I'm getting the error: 500 org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
From what I've read, it is caused by characters before the tag at the beginning of the XML doc.
How can I clean the begging of the XML object so that it can be sent correctly to the server ? I'm able to access nodes with sld_doc.find("node_name") but how can I check for invalid characters before the 1st node (<?xml>) ?
At the moment you are passing a jQuery object. Try unwrapping it, and setting the right dataType too:
$.ajax({
url: "geoserver/rest/styles",
type: 'POST',
data: sld_doc[0],
headers: {
"Content-Type": "application/vnd.ogc.sld+xml"
},
dataType: 'xml',
success: function (data) {a
console.log('success');
},
error: function (x, e) {
console.log('error');
}
});
This is what I am trying to do. On a home page.. say /home.jsp, a user clicks on a link. I read value of the link and on the basis of which I call a RESTful resource which in turn manipulates database and returns a response. Interaction with REST as expected happens with use of JavaScript. I have been able to get information from REST resource but now I want to send that data to another JSP.. say /info.jsp. I am unable to do this.
I was trying to make another ajax call within success function of parent Ajax call but nothing is happening. For example:
function dealInfo(aparameter){
var requestData = {
"dataType": "json",
"type" : "GET",
"url" : REST resource URL+aparameter,
};
var request = $.ajax(requestData);
request.success(function(data){
alert(something from data); //this is a success
//I cannot get into the below AJAX call
$.ajax({
"type": "post",
"url": "info.jsp"
success: function(data){
alert("here");
("#someDiv").html(data[0].deviceModel);
}
});
How do I go about achieving this? Should I use some other approach rather than two Ajax calls? Any help is appreciated. Thank You.
You can use the following function:
function dealInfo(aparameter) {
$.ajax({
url: 'thePage.jsp',
type: "GET",
cache: false,
dataType: 'json',
data: {'aparameter': aparameter},
success: function (data) {
alert(data); //or you can use console.log(data);
$.ajax({
url: 'info.jsp',
type: "POST",
cache: false,
data: {'oldValorFromFirstAjaxCall': data},
success: function (info) {
alert(info); //or you can use console.log(info);
$("#someDiv").html(info);
}
});
}
});
}
Or make the AJAX call synchronous:
function dealInfo(aparameter) {
var request = $.ajax({
async: false, //It's very important
cache: false,
url: 'thePage.jsp',
type: "GET",
dataType: 'json',
data: {'aparameter': aparameter}
}).responseText;
$.ajax({
url: 'info.jsp',
type: "POST",
cache: false,
data: {'oldValorFromFirstAjaxCall': request},
success: function (info) {
alert(info); //or you can use console.log(info);
$("#someDiv").html(info);
}
});
}
In this way I'm using.
"type": "post" instead of type: 'post'
Maybe it will help. Try it please. For Example;
$.ajax({
url: "yourURL",
type: 'GET',
data: form_data,
success: function (data) {
...
}
});
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.