Solved

simple ajax form sumbit

Posted on 2016-09-21
9
71 Views
Last Modified: 2016-09-22
Hello Experts,

Simple Ajax form submission.
The form is being submitted to 3rd part URL that collects the data.
The data is being received fine.
The problem: the success function does not seem to execute.

Please help :-) thank you!




$(document).ready(function() {
    
   var form = $('#guideDownload');

   $(form).on("submit", function(evt) {
      
      evt.preventDefault();	   
      submitForm();
      
   });

   function submitForm() {
   
      var formData = $(form).serialize();
      
      var first_name = $("#first_name").val();
      var last_name = $("#last_name").val();
      var email = $("#email").val();
      var mid = 'ls09khl';
      var source = 'guide';  
   
      $.ajax({
         type: "POST",
         url: "https://www.3rdpartwebsite?encoding=UTF-8",
         data: formData,
         success : function() {
           $(form).hide();		
           $( "#msgSubmit" ).html("Thank you for your inquiry");
         }
      });
   }
});

Open in new window

0
Comment
Question by:Refael
  • 4
  • 3
  • 2
9 Comments
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) earned 250 total points
ID: 41809825
I would recommend you debug by adding an error clause.
success : function() {
//..your current code
},
error: function(error) {
    console.log(error); //use F12 console to inspect
}

Open in new window


If that doesn't yield any results, try using a complete clause instead of error:

complete: function(response) {
     console.log(response); //use F12 console to inspect the response
}

Open in new window

0
 

Author Comment

by:Refael
ID: 41809840
Hi, I did not look at the console and now I see this message:

XMLHttpRequest cannot load http://www.thesiteireferto.com?encoding=UTF-8. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mysite.com' is therefore not allowed access.

(i changed the names of the sites as it is a client website).
0
 

Author Comment

by:Refael
ID: 41809848
I just checked the if the form data is sent to the URL and it is sent. The URL i am sending the data to is sent and i see the form variables all fine.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Refael
ID: 41809851
I used the complete function instead of success and it is working.
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 41810826
What does this do
 
$.ajax({
         type: "POST",
         url: "https://www.3rdpartwebsite?encoding=UTF-8",
         data: formData
      }).done(function() {
           $(form).hide();		
           $( "#msgSubmit" ).html("Thank you for your inquiry");
         }
      });
   }

Open in new window

Success has been deprecated in future versions of jQuery in favour of the above.
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41810902
According to that error, I'm surprised it's working for you.  That's a Cross Domain block from your browser.  There are quite a few methods for dealing with CORS:  http://techblog.constantcontact.com/software-development/using-cors-for-cross-domain-ajax-requests/
1
 
LVL 56

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41810996
Second zephyr_hex on this one missed the post with the error.

You need to add a header to the server that allows origin to be your domain
For example
Access-Control-Allow-Origin: *

Open in new window

If you don't have access to the server to make this change then you will be unable to make the request. If you do control the server then simply add the above header to your output to allow your AJAX script to call it from a different domain.
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41811014
The OP stated it was a 3rd party server, which I took to mean that it is not under the OP's control.
0
 

Author Closing Comment

by:Refael
ID: 41811061
Thank you all for your always great help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript Regex 7 20
Video Tutorial help 2 37
[form-control] Retain values after a POST action 21 32
Read text on Table 7 25
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question