Solved

simple ajax form sumbit

Posted on 2016-09-21
9
62 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

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

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 54

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sorting a string 1 28
ASP.NET gridview select textbox on focus 2 33
Jquery keyup 4 17
Grunt script for Build Process 1 25
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…

785 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