jQuery Ajax header not being passed - javascript

I want to send an AJAX call to a service with a couple of custom headers. While the request goes successfully, the headers are not being sent. The following is my code snippet:
func123: function (params) {
return $.ajax({
url: "http://localhost:8080/",
type: 'GET',
headers: {
"Contract": "1",
"Vector": "abc",
"Authorization": "xyz"
},
dataType: 'json',
contentType: 'application/json'
});
},

Related

Vue js making server call using vue-resource

I'm trying to figure out how to make the below server call using vue-resource. I not quite sure how to set the header and send data using Vue.$http.post
jQuery.ajax({
url: "http://url/",
type: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
},
contentType: "application/json",
data: JSON.stringify({
"email": "foo",
"password": "bar
})
})
You should be able to just do this:
Vue.http.post('http://dev-api.languagelink.com/api/auth/login', {
email: 'foo#bar.com',
password: 'foobar',
}).then(res => {
// Handle success response
})
vue-resource will automatically set the Content-Type header and stringifies the payload as JSON.
Try something like this :
this.$http.post('/url', data, {
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}).then(res => {
//do the stuff
});

How to do a service call for 'POST' method?

How to do a service call for 'POST' method for the payload with the headers described below?
Payload for the service:
{
"SupplierPOPrint": {
"ErrorFlag": "Status Code: 20, Message ID: 505",
"ResultMessage": "Integration Service: Unable to find a routing corresponding to the incoming request message."
}
}
headers: { "Accept": "application/json", "Content-Type": "text/xml", },
function poPrint() {
$.ajax({
url: '/wps/proxy/http/10.249.114.31:8009/soa-infra/resources/Supp‌​lierPortal/CreateSup‌​plierPOPrintService!‌​1.0/CreateSupplierPO‌​PrintService/Create'‌​,
type: 'POST',
dataType: 'json',
data: new XMLSerializer().serializeToString(updatedTemplate),
crossDomain: true,
success: function(poPrintData) {
console.log("poPrintData", poPrintData);
poPrintDetails = poPrintData.SupplierPOPrint;
console.log("poPrintDetails", poPrintDetails);
poPrintTemplate = generatepoPrint(poPrintData);
$("#poPrintOutput").html(poPrintTemplate);
},
error: function(data) {
console.log("Update Content Failed", data);
}
});
}

How to rewrite ajax get request to post request (jquery)?

I have a GET request:
$.ajax({
url: '/items?ids=' + value.join(','),
method: 'get',
dataType: 'json'
})
How can I create a POST request from this?
$.ajax({
url: 'url_here',
method: 'post',
data: {
fName1: value1,
fName2: value2,
...
},
dataType: 'json'
});
The way of post requests works are different from the get request, in http post request you need to pass the values in the body of your request as bellow :
$.ajax({
url: "/items",
contentType: "application/json",
type: "POST",
data: {
"id": $("#id").val(),
"value1": $("#value1").val(),
"value2": $("#value2").val()
}
});
you can use fiddler to debug and watch what going on the wire.

Ajax POST request issue with Flask Restless

I'm trying to use jQuery and Ajax to send a POST request to my API.
$.ajax({
url: '/api/Orders',
headers: {
contentType: "application/json"
},
type: "POST",
data: JSON.stringify({'description':'test'}),
});
When I'm using postman (chrome extension) the POST request is fine and everything works great.
If I'm trying to use the above code with AJAX the response is:
message:"Request must have "Content-Type: application/json" header"
I find it really weird because I set contentType : "application/json".
Did you have tried to set the property dataType to JSON?
Your code will looks like:
$.ajax({
url: '/api/Orders',
headers: {
contentType: "application/json"
},
type: "POST",
data: JSON.stringify({'description':'test'}),
dataType: "json"
});
After 6 hours i found the correct answer.
$.ajax({
url: 'http://127.0.0.1:5000/api/Orders',
headers: {
accepts: 'application/vnd.api+json'
},
contentType: "application/json",
type : 'post',
data: JSON.stringify({
'description' : 'test'
}),
dataType: "json",
success: function(data) {
console.log(data);
}
});

Paypal Java script integration

Want to integrate Paypal with my mobile web application. I tried to get the access token using client id and secret id but unable to get the access token.
Below is the sample Ajax call with I am making to retrieve the access token.
function getAccessToken(){
$.ajax({
url:"https://api.sandbox.paypal.com/v1/oauth2/token/",
type:"POST",
data : {"grant_type":"client_credentials"},
beforeSend: function (request)
{
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Accept-Language", "en_US");
request.setRequestHeader("Authorization", "abc XXXXX:XXXXXXXXXXXXX");
},
success: function(data) {
alert(data);
},
error: function(e, messgae,type){
alert("Error" + e +" "+messgae+" type "+type);
}
});
I am unable to retrive the access token from the server.
Can anyone please tell me how can I integrate Paypal with my mobile web application using java script?
after a series of try and fail I found the correct AJAX call:
$.ajax({
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"Authorization": "Basic "+btoa("**<Client ID>:<Secret>**")
},
url: "https://api.sandbox.paypal.com/v1/oauth2/token",
type: "POST",
data: "grant_type=client_credentials",
complete: function(result) {
alert(JSON.stringify(result));
},
});
You need to replace Client ID:Secret with the one that you find on your Developer Dashboard, for example AxxhGDh:hudh-X-h8qi
Above example doesn't works, below works for me:
var parameter = {
"grant_type": "client_credentials",
"username": "<username>",
"password": "<password>"
}
$.ajax({
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"Authorization": "Basic <your auth key>"
},
url: "https://api.sandbox.paypal.com/v1/oauth2/token",
type: "POST",
data: parameter,
complete: function (result) {
alert(JSON.stringify(result));
},
})

Categories