0

We are trying to create an add-on for a specific platform, this add-on will monitor the fields and post the fields as soon as the form is submitted. We don't have access over the form and aren't possible to change the HTML, so we tried to do it merely with HTML.

$(window).load(function () {
    $(document).ready(function () {
        $('.gui-button-action').click( function() {
            console.log('Button Clicked');
            var values = $('#post-form').serialize();       
            var baseURL = 'http://js.com/get?&';    
            $.get(baseURL+values, function(){});
        });
    })
})

The above script is fired when the #gui-button-action is clicked. However the request to the other server isn't made.

If I add one line of code, only an alert, the script fires the same and the "GET" request is fired to the other server.

$(window).load(function () {
    $(document).ready(function () {
        $('.gui-button-action').click( function() {
            console.log('Button Clicked');
            var values = $('#post-form').serialize();       
            var baseURL = 'http://js.com/get?&';    
            alert('test'); // line added
            $.get(baseURL+values, function(){});
        });
    })
})

What is the cause and how can I work around it?

Peter Fox
  • 1,239
  • 2
  • 11
  • 31
  • Google for Same Origin Policy. – Yury Tarabanko May 28 '14 at 18:39
  • Yury, but how does the alert make sense? – Peter Fox May 28 '14 at 18:39
  • Even with the alert, the `$.get` reponse is the real content that you are looking for? – gustavodidomenico May 28 '14 at 18:41
  • 1
    Adding alert allows you to make cross domain request?? Hmm, I'm sure it is impossible. Try to add alert to the success callback. I doubt it would be called. – Yury Tarabanko May 28 '14 at 18:41
  • The form should be submitted normally, but the data should be sent to another server as well. Adding a alert after the request works as well. – Peter Fox May 28 '14 at 18:42
  • I doubt it too. Probably the alert is just making you to wait a little more, and you think that the data had been sent. But actually the response is an error. – gustavodidomenico May 28 '14 at 18:43
  • $.get(baseURL+values, function(){alert( "Load was performed." );}); doesn't display an alert. – Peter Fox May 28 '14 at 18:45
  • 1
    Try adding an error handler, then you'll see an alert ;-) – jahroy May 28 '14 at 18:46
  • $.get(baseURL+values, function(){}).fail(function() { alert('woops');} Displays woops... So what is wrong. – Peter Fox May 28 '14 at 18:47
  • Adding the fail function + alert makes the request work. But just don't want the alert. – Peter Fox May 28 '14 at 18:50
  • 1
    See first comment: Google "Same Origin Policy" – Patrick May 28 '14 at 18:51
  • @Patrick or Jury please explain... – Peter Fox May 28 '14 at 18:55
  • What's to explain? Have you looked up the Same Origin Policy? Unless you can leave the alert in the success callback and see it every time the code runs, the request is not completing successfully. It doesn't matter what the other alerts are doing... – War10ck May 28 '14 at 19:09
  • @PeterFox From [Wikipedia](http://en.wikipedia.org/wiki/Same-origin_policy) (the first result for [a Google search on "same origin policy"](https://www.google.com/search?q=same+origin+policy)): "The same-origin policy... permits scripts running on pages originating from the same site – a combination of scheme, hostname, and port number – to access each other's DOM with no specific restrictions, but prevents access to DOM on different sites. **The same-origin policy also applies to XMLHttpRequest...**" – apsillers May 28 '14 at 19:12
  • But the script is hosted somewhere else. The for is on http://example.com and the JS script is on http://js.com and the get is made to http://js.com. – Peter Fox May 28 '14 at 19:14
  • See this post http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – Samuel May 28 '14 at 19:15
  • @PeterFox The origin of the script resource is not important. The origin of the browsing context (e.g., top-level page; what appears in your address bar) that runs the script is what counts as the requesting origin. – apsillers May 28 '14 at 19:17
  • But why is the alert causing it to work? – Peter Fox May 28 '14 at 19:23
  • @PeterFox Can you edit your question to explain how you are determining whether the request succeeds or fails? Your code as written doesn't behave differently in either case. – apsillers May 28 '14 at 20:05
  • Got a working solution. The script just only had to post/get data to another server. The answer of that server is not important. What we did is changed the logic. On document ready we prepend the content with an image, and when the form is submitted we replace the src of that url with serialised data from the form. As soon as that is replaced the new SRC is fired and we get the parameters. – Peter Fox May 28 '14 at 21:20

0 Answers0