1

Apparently what I am trying to do on the front-end is incredibly unusual - I want the web browser to do a navigation event to a URL, and submit some JSON as an HTTP Post.

Google says the rest of the world wants to Not Navigate using jquery $.ajax. But not me - I want to navigate and post json.

I already have the whole thing working with $.ajax, including creating the JSON. How do I get the JSON POSTed and also cause the browser to navigate at the same time?

Does jQuery have a command for this? Let's try to keep the solution in jQuery instead of adding more HTML to the page, if possible.

Thanks everyone!

user1445967
  • 1,520
  • 4
  • 14
  • 30
  • You can [redirect the browser](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) after getting a response from your AJAX call. Is there a reason you don't want to POST via a `
    `? It might help to show the code you've go so far to demonstrate the context.
    – showdev Apr 18 '19 at 00:02
  • I dont want to post via a form because the JSON is generated from the form data by javascript, it is not supplied by the user. I want the server response to be a page that understand the JSON that was just posted, instead of something more complex where I have to say 'hey remember that JSON I posted a while ago?' Unless there is a post-form-as-JSON convention that I missed, I don't think my existing code affects any of this and I'm trying to avoid the ajax-post-then-later-redirect convention that seems to be expected here. – user1445967 Apr 18 '19 at 00:08
  • You will have to have `CORS` access to the page you are submitting to. Do you? – StackSlave Apr 18 '19 at 00:08
  • @PHPglue I am coding the server side application in Spring MVC, so at this point I would say, probably :) – user1445967 Apr 18 '19 at 00:10
  • You should test code on the page the user is filling out the form on before its submission. After validation via JavaScript you should send AJAX to the Server, where the code must be validated again, because the Client can always be altered. After your Server validates code, set a session to test against when you navigate, then send back an AJAX response that lets you know all is good. If all is good, then go ahead and navigate using `location = 'yourURL.php'` or whatever, making sure you test for that session value, or redirect if it doesn't match. – StackSlave Apr 18 '19 at 00:20

0 Answers0