I am trying to alert the visitor of my webpage with a country code provided by a JSONP endpoint. However, I cannot seem to get this working! And all the answers on the web seem confusing!
The following is returned from the endpoint:
{
"pcCartCt": 0,
"pcCountryCode": "au",
"pcCountrySite": "WWW",
"pcExpDomCt": 0,
"pcFirstName": "",
"pcIsCdc": false,
"pcIsDiscount": false,
"pcIsGdEmployee": false,
"pcIsTrusted": false,
"pcLanguageCookie": "en-us",
"pcModalData": "",
"pcRepEmail": "",
"pcRepExt": "",
"pcRepName": "",
"pcSelectedCurrFullDesc": "United States Dollar $ (Transactional)",
"pcSelectedCurrType": "USD",
"pcSetLanguageLogo": true,
"pcShopperId": ""
}
For it to work, I first need to specify a prog_id
of "myprogid" and a callback function. In the callback, I need an alert message containing pcCountryCode
.
How do I do this?
The endpoint should return:
myprogid({
"pcCartCt": 0,
"pcCountryCode": "au",
"pcCountrySite": "WWW",
"pcExpDomCt": 0,
"pcFirstName": "",
"pcIsCdc": false,
"pcIsDiscount": false,
"pcIsGdEmployee": false,
"pcIsTrusted": false,
"pcLanguageCookie": "en-us",
"pcModalData": "",
"pcRepEmail": "",
"pcRepExt": "",
"pcRepName": "",
"pcSelectedCurrFullDesc": "United States Dollar $ (Transactional)",
"pcSelectedCurrType": "USD",
"pcSetLanguageLogo": true,
"pcShopperId": ""
});
I don't understand your question about parsing multiple parameters. JSONP just expects one special parameter: callback=myprogid
. This parameter should be used as the name of the function at the beginning of the response.
If you're after the JS to pull the JSON from the server, this might help. It uses jQuery's promise interface to return a AJAX promise from getData and when the downloading is complete the callback logs the information to the console (or alert).
function getData() {
return $.ajax({
url: // url,
type: 'GET',
dataType: 'jsonp'
data: { prog_id: 'myprogid' }
});
}
$.when(getData()).then(function (data) {
console.log(data.pcCountryCode);
});