Solved

simple ajax form sumbit

Posted on 2016-09-21
9
56 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 42

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
 

Author Comment

by:Refael
ID: 41809851
I used the complete function instead of success and it is working.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

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 42

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 52

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 42

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now