Making a HTTPS Post call in JQuery

I have an html button that navigates to a payment website through a https post action. I need to move that functionality into a named action so that I can call it in a series of actions in BF. I’m using JQuery. Here’s my JQuery:

`$.post('` `https://checkout.globalgatewaye4.firstdata.com/?id=pay_now_form_348fdc30d1` `',   // url`
`       { myData: 'x_login:"WSP-EASY-WBN@qgDgjA, x_show_form:"PAYMENT_FORM", x_fp_sequence: "1630585578265143399", x_fp_hash:"PNB-1.0-ec79af75e6e50b5db01d27ec9e2d6a55cc023b20", x_amount:"230.00", x_currency_code:"USD", x_test_request:"FALSE", x_relay_response:"", donation_prompt:"", button_code:"Pay Now 2% Noble Milled Gold"' }, // data to be submit`
`       function(data, status, jqXHR) {// success callback`
`                $('p').append('status: ' + status + ', data: ' + data);`
`        })`

Here’s the error I’m getting:
[Error] Failed to load resource: Cross-origin redirection to Payment - Error denied by Cross-Origin Resource Sharing policy: Origin https://ezgc.cranstonhost.com is not allowed by Access-Control-Allow-Origin. (pay, line 0)

Are you trying to open the payment website in a new window, navigate to it in the current window, or just get the response from the url and process it in javascript?

It would be great to open a new window.

I thought that might be the case, which is why I asked. I’ve done that before with the code below. I tried to extract the most relevant bits without leaving out anything important.

function openWindowWithPostRequest(url, windowName, windowFeatures, params) {
  var form = document.createElement("form");
  form.setAttribute("method", "post");
  form.setAttribute("action", url);
  form.setAttribute("target",windowName );
  for (var i in params) {
	if (params.hasOwnProperty(i)) {
	  var input = document.createElement('input');
	  input.type = 'hidden';
	  input.name = i;
	  input.value = params[i];
	  form.appendChild(input);
	}
  }
  document.body.appendChild(form);
  var w = window.open('', windowName ,windowFeatures);
  //form.target = windowName ;  // redundant with setAttribute above?
  //console.log(form);
  form.submit();
  document.body.removeChild(form);
  return w;
}

var params = {
	'x_login': "WSP-EASY-WBN@qgDgjA"
	// TODO: add the rest of your data to send here
};

// NOTE: this has some business logic in it that might not apply to you, but I left it since it was something I overlooked at first, and wouldn't be obvious until it failed
var w = _.get(window.xpresspay, invoice.id);
if (w && w.closed === false) {
	console.log('selecting previously open xpresspay window')
	w.focus();
} else {
	w = openWindowWithPostRequest(url, 'xpresspay-' + invoice.id, '', params);
	if (w) {
		if (! window.xpresspay) {
			window.xpresspay = {};
		}
		window.xpresspay[invoice.id] = w;
		w.focus();
		// NOTE: paymentLoading is cleared after closing the modal
	} else {
		console.log('Browser has blocked window')
		model.paymentLoading = false
		EventBus.$emit('processNamedAction', {
			action: "namedAction",
			name: "xpresspay_blocked",
			options: {}
		});
	}
}

Thank you! I will dig into it!

It worked! Thank you!

1 Like