-1

EDIT - Resolved.

Thank you everyone for taking the time to respond. After hours of researching I was directed to CORS, but turns out I should be looking at JSONP the whole time. I read a few tutorials, and I think I understand. Thanks again.

What I'm trying to do is to pass user input from a form to my server, which is different from the server the form is located at. I won't make this too long to read, so I'll jump straight away to code.

In the following javascript code, I'm getting elements by tagName for security issues, I don't want to use name attributes in my html form. Then I'm storing data retrieved into a JSON datatype which I'll then call an ajax function on it.

    function processForm() {
        var i, userInput, inputType;
        var name, creditNo, cvv, month, year;
        var inputs = document.getElementsByTagName("input");
        for (i=0; i < inputs.length; i++){
            inputType = inputs[i].getAttribute('data-tajer');
            userInput = inputs[i].value;
            if (inputType == 'name') {
                name = userInput;
            }
            else if (inputType == 'cc') {
                creditNo = userInput;
            }
            else if (inputType == 'cvv') {
                cvv = userInput;
            }
            else if (inputType == 'month') {
                month = userInput;
            }
            // year
            else {
                year = userInput
            }
        }
        var myJASON = {"name": name, "cc": creditNo, "cvv": cvv, "month": month, "year": year};
        var strJSON = JSON.stringify(myJASON);
        ajaxCall(strJSON);
    }

Now the ajax call, which should be trivial. Only difference here is my url is on a different server.

    function ajaxCall(param){

        var url = 'http://blahbalh';

        // jquery code snippet
        var ajaxRequest; 

        try{
            ajaxRequest = new XMLHttpRequest();
        } catch (e){
            try{
                ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e){
                    alert("Please update your browser biatch!");
                    return false;
                }
            }
        }
        // Create a function that will receive data sent from the server
        ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                var ajaxDisplay = document.getElementById('ajaxDiv');
                ajaxDisplay.innerHTML = ajaxRequest.responseText;
            }
            else {
                alert("failed");
            }
        }
        ajaxRequest.open("POST", url, true);
                    // param is the JSON data.
        ajaxRequest.send(param);
    }

Basically what happens now is that I get back a status of 0 and readyState of 1. Can you guys spot what I'm doing wrong? Bear in mind, I had it in jQuery first, but it didn't work either. I'm open for any solution and suggestions.

I'll provide the html form for convenience.

    <form id="paymentForm">
        <h3>Payment Form</h3>
        <h4>Please fill in the form below, * are required fields.</h4>
        <div>
            <label>
                <span>Name: *</span>
                <input placeholder="Please enter your name as it appears on your credit card." id = "name" type="text" data-tajer="name" tabindex="1" required autofocus>
            </label>
        </div>
        <div>
            <label>
                <span>Credit Card: *</span>
                <input placeholder="Please enter credit card number" type="text" data-tajer="cc" tabindex="2" required>
            </label>
        </div>
        <div>
            <label>
                <span>CVV: *</span>
                <input placeholder="Please enter CVV code found on the back of your card" type="text" data-tajer="cvv" tabindex="3" required>
            </label>
        </div>
        <div>
            <label>
                <span>Expiry Month: *</span>
                <input placeholder="Please enter the expiry month of your credit card" type="text" data-tajer="month" tabindex="4" required>
            </label>
        </div>
        <div>
            <label>
                <span>Expiry Year: *</span>
                <input placeholder="Please enter expiry year of your credit card" type="text"  data-tajer="year" tabindex="5" required></input>
            </label>
        </div>
        <div>
            <button onclick="processForm()">Process Payment</button>
        </div>
    </form>
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
zsawaf
  • 1,921
  • 16
  • 24
  • this may be a duplicate from http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain. Check it to see if the answers there fit your needs :) – jvilhena May 30 '13 at 18:06
  • if you have access to remote server and you don't need it working on old browsers, its better to use CORS – igor May 31 '13 at 06:59

2 Answers2

3

Cross Origin Access is the problem you can't do that unless you allow the domain to be whitelisted or use a JSONP

Say you're on domain abc.com, and you want to make a request to domain xyz.com. To do so, you need to cross domain boundaries, a no-no in most of browserland.

The one item that bypasses this limitation is tags. When you use a script tag, the domain limitation is ignored, but under normal circumstances, you can't really DO anything with the results, the script just gets evaluated.

Enter JSONP. When you make your request to a server that is JSONP enabled, you pass a special parameter that tells the server a little bit about your page. That way, the server is able to nicely wrap up its response in a way that your page can handle.

jsonp is really a simply trick to overcome XMLHttpRequest same domain policy. (As you know one cannot send ajax(XMLHttpRequest) request to a different domain.)

So - instead of using XMLHttpRequest we have to use script html tags, the ones you usually use to load js files, in order for js to get data from another domain. Sounds weird?

Thing is - turns out script tags can be used in a fashion similar to XMLHttpRequest!

Josh Bedo
  • 3,410
  • 3
  • 21
  • 33
  • cheers dude, awesome answer. I'm going to rewrite my code back to jQuery, as I read it fits well with JSONP. – zsawaf May 31 '13 at 06:56
  • Please provide the source from where you get it like http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about Otherwise make your contribution – Viswanath Lekshmanan Aug 30 '14 at 17:33
1

You can't send an AJAX request to another server, see http://en.wikipedia.org/wiki/Same_origin_policy A workaround is to use JSONP: http://en.wikipedia.org/wiki/JSONP

Reeno
  • 5,720
  • 11
  • 37
  • 50