简体   繁体   中英

Form action vs AJAX request - sending JSON

On a fairly major PHP/Drupal website (this is not a Drupal question) we have the task of allowing a deeplink into a page where lots of data must be provided by the calling site.

Cross-domain won't be an issue as we control both ends and can adapt as needed.

So what we want is that clicking Submit for a form on the originating server [A] will send the POST data to the receiving server [B]. That's the easy bit.

The complexity comes because there is a ton of non-user data that must be sent as well (doesn't matter why or where it comes from). So (i) there's additional data to be added to the POST payload, and (ii) the originating server [A] is node.js and they want to send JSON-encoded data and not use form-urlencoded.

And they (the JS guys) want to do it in one jump. User clicks the form on [A], they build the payload, POST to the deeplink URL and the browser displays the new page from [B].

I can handle it at the PHP end, but I can't get my head around it conceptually. I don't think it's possible.

As I understand it, the form action URL is the key. It's this that redirects the browser, sending the form-urlencoded data either in the URL or in the body. (Can't do it as JSON as we know.)

But it seems to me that if they do anything to interrupt the handling of the form on [A] the browser won't be redirected to [B] and it just becomes an AJAX call.

So the question is: Is it possible to modify the payload of a form and still get the same behaviour, redirecting the browser to the new page?

(The solution, if this is impossible, is relatively simple, an AJAX call to set everything up, which returns a URL, redirect the browser to the supplied URL.)

No, it isn't.

The closest you could come would be to use Ajax to make the request and include, in the response, a URL (with a token to identify the request you made with Ajax in it) which you can assign to location.href in XHR's load event.

let me understand your issue:

on the server A (nodejs) there is a button and after clicking on it you would like that some data (plus lots of other data) to B send to second server B which will display totally different page

your (nodejs) guys want to send you data directly from the nodejs, json encoded

i my opinion you have 2 options

first - not so good:

nodejs displays form on server A, with additional data set as hidden input field (encoded how you wish), in the <form> tag action attribute pointing to the server B (deep link), then server B gets the data, decode it and displays final page.

pros: - simplicity

cons: - you expose all data and link to server B to the external user (data is sent to server B via browser), - there may be a lot of data (and long transmission), - data can be manipulated (by attacker)

second option, better:

is that server A shows html form , and receives data from this form, then server A is sending data to the server B directly, then response from server B is received by server A and sent as response to posted form.

pros: - secure (you do not expose additional data and deep-url is hidden) - much faster as server A and B can communicate over LAN, not the internet - you can hide existence of server B

cons: - complexity in processing if you want to switch user to server B - in such a case you will need to return (as an example) url to the page on server B, so server A will send HTTP Redirect to this url as a response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM