0

Yes I am new to JSON.

Im setting up a paymentsolution for a web shop. The customer press a button indicating he wants to receive a link via mail or SMS and make the payment at a later date.

Parameters is sent to the Payment Service Provider using HTTP(s) POST to https://{psp-url}/admin/link_pay that returns the link in a JSON response.

That response is (depending on browser) displayed as plain text whereas I of course want to grab the response and parse it - and hense send a mail/SMS to the customer.

It looks as simple as this:

{"url": "https://{psp-url}/pay/link/54abc4ab4abcd56de3fghijk", "id": "54abc4ab4abcd56de3fghijk", "result": "Success"}

My question is NOT how to parse. But - HOW do I do to get it into a variable - the only portion i really need is the "id".

The documentation from my PSP is poor (non existing) and I would really need a code example - but havent found one despite 36 hours of searching

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • *"HOW do I do to get it into a variable"* - You make an Ajax request from JavaScript and the response then comes back to the JS and not as a new page. (As compared to a standard form submission, where the response replaces the current page.) From there you can process it as a string to parse it and get an object and extract the `id` value. Perhaps [MDN can help](https://developer.mozilla.org/en/docs/AJAX). – nnnnnn Feb 01 '15 at 06:25

1 Answers1

0

Assuming you're using jQuery:

$.ajax({
    url:"https://{psp-url}/admin/link_pay",
    success:function(data){     
        try{
            var json = JSON.parse(data);
            console.log("The id is "+json.id);                      
        }catch(e){
            console.log("I didn't get valid JSON!");
        }
    }
});
ReallyMadeMeThink
  • 1,061
  • 7
  • 11
  • OK - so now i get: XMLHttpRequest cannot load https://{psp-url}/admin/link_pay. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 405. Resource interpreted as Document but transferred with MIME type application/json: "https://{psp-url}/admin/link_pay". - And I do acknowledge being out of my comfort zone here – Bob Johnston Feb 01 '15 at 07:23
  • Cross-origin policy is a security restriction enforced by browsers. If you upload your client-facing page on the same host where you're currently serving your php page you won't get this error. For local development you can try disabling this restriction from your browser or take a look at http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – ReallyMadeMeThink Feb 01 '15 at 08:25
  • That does not seem to be the issue at all - with same result.XMLHttpRequest cannot load https://{psp-url}/admin/link_pay. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.mypage.com' is therefore not allowed access. The response had HTTP status code 405. Resource interpreted as Document but transferred with MIME type application/json: "https://{psp-url}/admin/link_pay". – Bob Johnston Feb 01 '15 at 08:36
  • Or actually - when transmitting I only get this: Resource interpreted as Document but transferred with MIME type application/json: "https://{psp-url}/admin/link_pay". – Bob Johnston Feb 01 '15 at 08:44
  • Forgetting AJAX are you able to hit the php page in question? – ReallyMadeMeThink Feb 01 '15 at 08:51
  • It is actually an .asp-page - and yes- that page loads with the form (and button to press for the customer) - and no errors on the console. When the form is submitted the result is as described above in my OP - the JSON-result is displayed in plain text with https://{psp-url}/admin/link_pay in the browser address field – Bob Johnston Feb 01 '15 at 08:59