1

I'm working with a script to auto populate and submit an external form. The remote website seems to use Ext.Ajax.request to validate the form. My first attempt didn't work because of cross-domain issue : No 'Access-Control-Allow-Origin' :(

code source of the remote form:

<form action="http://domain.com/loginaction.ashx" method="post">
    <input type="text" name="username" value=""/>
    <input type="password" name="passwd" value=""/>
   <button id="submitbtn"  onclick="validate();">Log In</button>                      
</form>

validate() function is defined in js file.

Plz guys! what is the easiest way to send cross-domain requests?

1 Answer 1

0

I recommend you use JQuery's $.ajax() call to send data to a URL.

Your HTML: I removed the action and method in the HTML, and moved them into the JavaScript. Also, notice I added IDs (id="") to the elements, so we can call them by using JQuery.

<form>
    <input id="name" type="text" name="username" value=""/>
    <input id="pass" type="password" name="passwd" value=""/>
    <button id="submitbtn">Log In</button>
</form>

Your JavaScript (Using JQuery):

// Call validateForm() when button is clicked

$("#submitbtn").on("click", validateForm);

function validateForm(){
    var inputName = $("#name").val(); // Get value of name
    var inputPass = $("#pass").val(); // Get value of pass

    // Make sure values aren't empty
    if(inputName != "" && inputPass != ""){ 

        // Assign values to parameters object
        var params = {
            "entry-1" : inputName,
            "entry-2" : inputPass
        };

        // Send the parameters to the selected URL
        var ajaxPost = $.ajax({
            url: "http://domain.com/loginaction.ashx",
            type: "POST",
            data: params
        });

        // What to do upon success
        ajaxPost.done(function(data, textStatus){
            console.log("Success!");
            console.log("data");
        });

        // What to do upon error
        ajaxPost.fail(function(jqXHR, textStatus){
            console.log("Failure!");
            console.log("jqXHR");
        });
    }
}
3
  • I tried this but it didnt work... always same error : No 'Access-Control-Allow-Origin' header is present on the requested resource....
    – Azhwani
    Dec 25, 2014 at 11:16
  • One other thing that I need to mention here is that I have no control over the remote website...
    – Azhwani
    Dec 25, 2014 at 12:37
  • Oh, then it looks like the remote server isn't allowing you to submit your information because it's coming from an unknown source. It's a security feature to prevent unwanted access.
    – M -
    Dec 25, 2014 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.