Avatar of Paul Konstanski
Paul KonstanskiFlag for United States of America

asked on 

Ajax Call to Identical External Script Doesn't Fire

I have a script that works perfectly when I run it from my local machine.  But when I try to run it with a call to an external server it returns a failure. I know the script actually arrives at my remote machine because I see it's arrival in the logs, but it doesn't properly send back the success notification.

The application is a webpage that has a simple "contact us" link with two inbound fields - name and email.  The standard html page makes a call to a file called "contact_me.js" which contains this code:

$(function() {
    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
	    var rmturl = "././mail/contact_me.php";
	    // var rmturl = "https://myserver.com/contact_me.php";
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: rmturl,
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

Open in new window

On line 10/11  I define the source file as the variable "rmturl",  In both cases it is simply calling the "contact_me.php" file that simply has one line of code "return true" (I simplified for testing purposes).

When I run to the local file (././mail/contact_me.php), it get the success message to fire (line 37).
But when I run it to the remote server I get the message that says "Sorry, mail server isn't replying". (line 49)

What might be the reason for why this works off the local script but not the remote server? Is there some other parameter that needs to be added to signify this is remote? Or some other protocol?

Thanks.
AJAXJavaScriptJSON

Avatar of undefined
Last Comment
Paul Konstanski
ASKER CERTIFIED SOLUTION
Avatar of Paul Konstanski
Paul Konstanski
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Paul Konstanski

ASKER

Should have looked at this right from the start.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo