Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simple ajax form sumbit

Posted on 2016-09-21
9
Medium Priority
?
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 44

Accepted Solution

by:
zephyr_hex (Megan) earned 1000 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
Industry Leaders: 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 59

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 44

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 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 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 44

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

650 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