Track visit with Javascript - jQuery on HTML website - javascript

I'm trying to get this code to track user visit but I can't manage to get it to work.
When I do this in Postman, I get successful, but to put it in the HTML I get nothing.
How do I make the script activate when user visit website? I need to pass on the values to a /registerLead on form submission afterwards.
<script>
var settings = {
"url": "https://api31.trackinglink3.com/SignalsServer/api/trackVisit",
"method": "GET",
"timeout": 0,
"headers": {
"apikey": "apikey",
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"devicetype": "",
"Offer": "",
}
};
</script>

Just use ajax function for this. check snippet below..
$(function(){
$.ajax({
method: "GET",
url: "https://api31.trackinglink3.com/SignalsServer/api/trackVisit",
"timeout": 0,
"headers": {
"apikey": "apikey",
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"devicetype": "",
"Offer": "",
}
})
.done(function() {
// your done code here
});
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Related

Citi Bank Open API Authorization

I am trying to use the Citi Bank Open API in my React Native mobile app. I was able to call the API to let user sign in with their Citi Bank account and get the client authorization code. However, when I was trying to call another API for validating the code I got, it returned a weird error without any description.
I suspect problems may be caused by:
my app is still in development stage
I didn't do the calling on the redirect uri, but the domain is definitely the same
The API is supposed to return an access token for further interaction, but all I got is the error. I wanna know what might be the possible mistakes.
This is my first time trying to use an open API inside my app. Any suggestion will be much appreciated.
Official docs from Citi (may need to register an account):
https://sandbox.developerhub.citi.com/api/hong-kong/retail-bank/identity-security/authorize/documentation
Calling the API:
fetch(
"https://sandbox.apihub.citi.com/gcb/api/authCode/oauth2/token/hk/gcb",
{
method: "POST",
body: new URLSearchParams({
grant_type: "authorization_code",
code: auth_code,//got from the response of GET /authCode/oauth2/authorize
redirect_uri: "http://localhost:19000/redirect",
}),
headers: new Headers({
"Cross-Domain": true,
Accept: "application/json",
Authorization:
"Basic " + btoa(CITI_CLIENT_ID + ":" + CITI_CLIENT_SEC),
"Content-Type": "application/x-www-form-urlencoded",
}),
}
)
.then((data) => {
console.log(data);
data.json();
})
.then((result) => {
console.log(result);//undefined
});
Response:
Response {
"_bodyBlob": Blob {
"_data": Object {
"__collector": null,
"blobId": "f09ce99b-1a3b-4251-9a73-5a4a809a6296",
"offset": 0,
"size": 88,
},
},
"_bodyInit": Blob {
"_data": Object {
"__collector": null,
"blobId": "f09ce99b-1a3b-4251-9a73-5a4a809a6296",
"offset": 0,
"size": 88,
},
},
"bodyUsed": false,
"headers": Headers {
"map": Object {
"access-control-allow-credentials": "true",
"access-control-allow-headers": "",
"access-control-allow-methods": "",
"access-control-allow-origin": "",
"access-control-expose-headers": "",
"connection": "close",
"content-length": "88",
"content-type": "application/json",
"date": "Thu, 24 Mar 2022 06:37:39 GMT",
"set-cookie": "CITI_SITE=gtdc; expires=Thu, 24-Mar-2022 06:57:39 GMT; path=/; domain=sandbox.apihub.citi.com; secure; HttpOnly",
"x-akamai-citisite": "GTDC",
"x-backside-transport": "FAIL FAIL,FAIL FAIL",
"x-global-transaction-id": "6e68aa42623c11b387504e5d",
},
},
"ok": false,
"status": 400,
"statusText": "",
"type": "default",
"url": "https://sandbox.apihub.citi.com/gcb/api/authCode/oauth2/token/hk/gcb",
}

AJAX POST throws a 415 Unsupported Media Type Error with message 'Request must have "Content-Type:application/vnd.api+json"'

I have an application that on click of a button, it makes an ajax post request to a flask backend(flask restless). As mentioned, I get an error with the message "Request must have 'Content-Type:application/vnd.api+json'", even though I have that same statement as part of my header.
Pointer, the post request gets posted successfully on the postman.
$(function() {
$('#add_details').on('click', function() {
$.ajax({
type: 'POST',
url: 'http://127.0.0.1:5000/api/v1/user',
data: {
"data": {
"type": "user",
"attributes": {
"first_name": "first",
"last_name": "second",
"email_address": "user43#gmal.acbde",
"phone_number": "4202024234",
"gender": "FEMALE"
}
}
},
headers: {
Accepts: 'application/vnd.api+json',
ContentType: 'application/vnd.api+json',
},
dataType: "json",
success: function(newUser) {
console.log('success', newUser)
},
error: function() {
alert('error encounted while adding user');
}
});
});
});
headers: {
Accepts: 'application/vnd.api+json',
ContentType: 'application/vnd.api+json',
},
I noticed an extra comma at the end of ContentType. Have you tired removing that?

AJAX POST request Using Variables in the options

I'm having an issue with making a post request with a project I created to take in contact info from a form. When I run the function to make the post request I get nothing back in the console. heres the code:
function postContact(token) {
firstName = $('#agentfirst').val();
lastName = $('#agentlast').val();
workNumber = $('#agentwork').val();
cellNumber = $('#agentcell').val();
faxNumber = $('#agentfax').val();
agentEmail = $('#agentemail').val();
agentLicense = $('#agentlicense').val();
companyName = $('#companyname').val();
streetAddress = $('#companyaddress').val();
addressCity = $('#companycity').val();
addressState = $('#companystate').val();
addressZip = $('#companyzip').val();
companyLicense = $('#companylicense').val();
var data = `{
"categoryId": 9,
"companyName": "${companyName}",
"personalContactLicense": {
"licenseNumber": "${companyLicense}"
},
"businessContactLicense": {
"licenseNumber": "${agentLicense}"
},
"noSpam": true,
"firstName": "${firstName}",
"lastName": "${lastName}",
"accessLevel": "1",
"currentMailingAddress": {
"street1": "${streetAddress}",
"city": "${addressCity}",
"state": "${addressState}",
"zip": "${addressZip}"
},
"workPhone": "${workNumber}",
"mobilePhone": "${cellNumber}",
"faxNumber": "${faxNumber}",
"businessEmail": "${agentEmail}"
}`;
var options = {
"async": true,
"crossDomain": true,
"url": "https://api.elliemae.com/encompass/v1/businessContacts",
"method": "POST",
"headers": {
"Content-Type": "application/javascript",
"Authorization": token
},
"processData": true,
"data": data
};
console.log(options.data);
$.ajax(options).done(function (response) {
console.log(response);
});
}
I dont if the problem is me using varibles in the options for the ajax call but any help or being pointed in the right direction would greatly appreciated.
If you have form, you can simply do
var form_data = $('#yourformId').serialize();
And on your ajax options, simply include this
data: form_data,
I am not sure, how you are handling this ajax call and how you are preparing your response. You can debug to see where you is the problem.

how do I grab data from an API that returns CSV file

I am working with Pluralsight API and their API responds CSV files as results, which I would like to grab this data from response and print out.
this is the URL of the Pluralsight API:
https://app.pluralsight.com/plans/api/reports/docs
and this is the code I am trying to do:
request
.get('https://api.pluralsight.com/api-v0.9/users?planId=x&token=y')
.on('response', function (response) {
res.json(response);
})
The response does not show any data except for the data as below:
{
"statusCode": 200,
"headers": {
"cache-control": "private",
"content-disposition": "attachment; filename=Users.csv",
"content-type": "text/csv",
"date": "Fri, 27 May 2016 03:42:06 GMT",
"ps-build": "2016.5.1849.0",
"ps-node": "0Q5JR",
"ps-responsetime": "00:00:00.1406230",
"content-length": "11391",
"connection": "Close"
},
"request": {
"uri": {
"protocol": "https:",
"slashes": true,
"auth": null,
"host": "api.pluralsight.com",
"port": 443,
"hostname": "api.pluralsight.com",
"hash": null,
"search": "?planId=x&token=y",
"query": "planId=x&token=y",
"pathname": "/api-v0.9/users",
"path": "/api-v0.9/users?planId=x&token=y",
"href": "https://api.pluralsight.com/api-v0.9/users?planId=x&token=y",
},
"method": "GET",
"headers": {}
}
}
Is there anyway that I get get the file from attachment and extract it?
Thanks
Request body is accessible through data and end events.
var body = [];
request
.get('https://api.pluralsight.com/api-v0.9/users?planId=x&token=y')
.on('data', function (chunk) {
body.push(chunk);
})
.on('end', function () {
body = body.join('');
// body now contains csv contents as a string
});

AJAX POST , having problems with "data" field

I am using auto generated the ajax with Postman 3, so you can tell if it's the correct way to do it, but this isn't my main question. Now, when I type the "data" field like this, it works
var settings = {
"async": true,
"crossDomain": true,
"url": "https://baas.kinvey.com/appdata/kid_B1BHxEN7/people/",
"method": "POST",
"headers": {
"authorization": "Basic Z2VzaGE6MTIzNA==",
"content-type": "application/json",
"cache-control": "no-cache",
"postman-token": "8de908df-f970-524c-eb8b-d2f7f621a3ac"
},
"processData": false,
"data": "{\"name\":\"Peter\",\"id\":11}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
Now my question is, how do I put custom parameters in the "data" field.
I want something like this
var arr = [{"name": "Peter", "id": 12}, {"name": "Demeter", "id": 15}];
var settings = {
"async": true,
"crossDomain": true,
"url": "https://baas.kinvey.com/appdata/kid_B1BHxEN7/people/",
"method": "POST",
"headers": {
"authorization": "Basic Z2VzaGE6MTIzNA==",
"content-type": "application/json",
"cache-control": "no-cache",
"postman-token": "e37d3063-406d-02c3-803f-0f32666b9d70"
},
"processData": false,
data: {"name": arr[0].name, "id": arr[0].id}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
When i send this POST request it gives me back 400(Bad request)
jQuery does not format your request as JSON but only as data url. You have to do this yourself:
data: JSON.stringify({"name": arr[0].name, "id": arr[0].id})
You need to convert the string to JSON format using stringify. Here is link to explanation:
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Categories

Resources