How to post json ajax request to web API - javascript

I'm unable to get json data from server side. The script calls the server method but no json data returned.
$(document).ready(function() {
$("#SendMail").click(function() {
$.ajax({
url: "http://localhost:2457/SendMail/SendMail/",
dataType: 'json',
type: 'POST',
data: "{htmlTemplate:" + "ABC" + "}",
//crossDomain: true,
//contentType: "application/json; charset=utf-8",
success: function(data, textStatus, xhr) {
console.log(data);
alert('Successfully called');
},
error: function(xhr, textStatus, errorThrown) {
// console.log(errorThrown);
}
});
});
});
Function SendMail(htmlTemplate As String) As String
Dim fname As String = Request.Form("htmlTemplate1")
Dim lname As String = Request.Form("lname")
Dim cmdSendMail As New SendMailCommand()
Return "A"
End Function

<script>
$(document).ready(function () {
$("#SendMail").click(function() {
$.ajax({
url: '/SendMail/SendMail/',
dataType: 'text',
type: 'POST',
data:JSON.stringify({htmlTemplate: "ABC"}),
//crossDomain: true,
contentType: "application/json; charset=utf-8",
success: function (data, textStatus, xhr) {
console.log(data);
alert('Successfully called');
},
error: function (xhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
});
});
</script>

Related

Firebase Dynamic Link Creation With JavaScript

var object={
"longDynamicLink": "https://[APP_NAME].page.link/?link=[LINK_HERE]",
"suffix":{
"option":"SHORT"
}
}
$.ajax({
url: 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=[KEY_HERE]',
type: 'POST',
dataType: "json",
data: object,
success: function(response, textStatus, jqXHR) {
alert(response.shortLink);
},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus, errorThrown);
}
});
The above code works if the "suffix" is deleted from the request. That makes an "UNGUESSABLE" url, but I want a short URL. As stated in the documentation at https://firebase.google.com/docs/dynamic-links/rest?authuser=0 I added the suffix option parameter, but it results with a 400 response. Any ideas why?
I haven't ever tried this but, ...
POST https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=api_key
var params = {
"longDynamicLink": "https://example.page.link/?link=http://www.example.com/&apn=com.example.android&ibi=com.example.ios",
"suffix": {
"option": "SHORT"
}
}
$.ajax({
url: 'https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=[KEY_HERE]',
type: 'POST',
data: jQuery.param(params) ,
contentType: "application/json",
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});

How to get response from ajax success call

I want to get response of ajax success call in variable 'response' from where I called function .
Call of function:
var response = ExecuteAction(Id,Entityname,Processname);
Function:
function ExecuteAction(entityId, entityName, requestName) {
$.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
datatype: "xml",
url: serverUrl + "/XRMServices/2011/Organization.svc/web",
data: requestXML,
async:false,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
},
success: function (data, textStatus, XmlHttpRequest) {
debugger;
if (XmlHttpRequest.status === 200) {
var response = $(XmlHttpRequest.responseText).find('b\\:value').text();
return response;
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
Please suggest me answer.
This is not how JS asynchronous functions are meant to be used. You should be using callbacks, unless you're using es6 in which you should use promises. But if for some reason you absolutely had to get it to work, you could do something like this (note, i did not test this).
function ExecuteAction(entityId, entityName, requestName) {
var response;
$.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
datatype: "xml",
url: serverUrl + "/XRMServices/2011/Organization.svc/web",
data: requestXML,
async: false,
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
},
success: function(data, textStatus, XmlHttpRequest) {
debugger;
if (XmlHttpRequest.status === 200) {
response = $(XmlHttpRequest.responseText).find('b\\:value').text();
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
while (!response);
return response;
}
Here's how to use a callback with the function
function ExecuteAction(entityId, entityName, requestName, callback) {
var response;
$.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
datatype: "xml",
url: serverUrl + "/XRMServices/2011/Organization.svc/web",
data: requestXML,
async: false,
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
},
success: function(data, textStatus, XmlHttpRequest) {
debugger;
if (XmlHttpRequest.status === 200) {
response = $(XmlHttpRequest.responseText).find('b\\:value').text();
callback(null, response);
}
},
error: function(XMLHttpRequest, textStatus, error) {
alert(error);
callback(error);
}
});
}
called as such
var response = ExecuteAction(Id,Entityname,Processname, function(err, result) {
if (err) console.log('whoops, error', err);
console.log('I will print upon completion', result);
});
Your problem is that you are using a sync function when AJAX is async,
you can use a Promise -
return new Promise( (resolve, reject) => {
$.ajax({
type: "POST",
contentType: "text/xml; charset=utf-8",
datatype: "xml",
url: serverUrl + "/XRMServices/2011/Organization.svc/web",
data: requestXML,
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
XMLHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
},
success: function (data, textStatus, XmlHttpRequest) {
if (XmlHttpRequest.status === 200) {
var response = $(XmlHttpRequest.responseText).find('b\\:value').text();
resolve(response);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
reject(errorThrown);
}
});
and then use it like that
var responsePromise = ExecuteAction(Id,Entityname,Processname);
responsePromise.then( (response) => {
console.log(response)
},
(error) => {
console.log(error)
});

jquery ajax ParseError with Musixmatch API

I am using jQuery 1.11.3 with the following code:
$.ajax({
type: "GET",
data: {
apikey: apiMusixkey,
q_track: q,
page_size: 10
},
url: "http://api.musixmatch.com/ws/1.1/track.search",
dataType: "jsonp",
contentType: 'application/json',
success: function(data) {
//console.log(json);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
I am getting the error:
parseError... [] was not called
What am I doing wrong?
Looks like you are missing a few things on your ajax. You need to specify the name of the callback function to handle the jsonp. Also, there's a format parameter you need to use with the musixmatch api. Checkout this plunker: http://plnkr.co/edit/XW6TFUJquW8o8EVpEEgU?p=preview
$(function(){
$.ajax({
type: "GET",
data: {
apikey:"309788821d050a0623303261b9ddedc4",
q_track:"back to december",
q_artist:"taylor%20swift",
f_has_lyrics: 1,
format:"jsonp",
callback:"jsonp_callback"
},
url: "http://api.musixmatch.com/ws/1.1/track.search",
dataType: "jsonp",
jsonpCallback: 'jsonp_callback',
contentType: 'application/json',
success: function(data) {
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
});

Getting string serves reply ajax and jquery

i got from my customer string that I need to get to the server and get an answer.
this is the string:
http://XXX.YYY.DDD.WWW:8082/My_ws?Myapp=Myprice&Mydip=123&itemno=123456
and i get any string as Result.
i try this in ajax and jQuery:
$.ajax({
ServiceCallID: 1,
url: MyString,
type: 'POST',
data: '{"itemno": "' + itemno+ '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success:
function (data, textStatus, XMLHttpRequest) {
ALL = (data.d).toString();
}
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
how to "talk" with the server and get the Result from my string ?
thanks

Why json data of ajax call ended up as querystring parameter?

I have the following ajax call. I want to send out the data in jason format. However I noticed in Fiddler that the data is converted to query string parameters. What I am doing wrong?
$.ajax({
type: "GET",
url: "StatusService.svc/CheckStatus",
data: JSON.stringify({"companyName":"paymins"}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('ok!');
alter(data.toString());
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus + ' / ' + errorThrown);
}
});
Change the type of your request to a post.
$.ajax({
type: "POST",
url: "StatusService.svc/CheckStatus",
data: JSON.stringify({"companyName":"paymins"}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('ok!');
alter(data.toString());
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus + ' / ' + errorThrown);
}
});
Get cannot contain a body. Use post for that.

Categories

Resources