简体   繁体   中英

ajax jquery php submit

i have a simple form in php that has a user textbox and pass textbox we'll call it login.php. I have another php page, let's call it sendtologin.php, that i want to 'send' the credentials via jquery ajax to post to login.php. The login.php is on server1 and sendtologin.php is on server2. when i try to load the sendtologin.php i get a status code of 0 and error. here is my script.

$(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "http://server1/login.cgi",
            data: { lang_changed: "no", username: "me", password: "me" },
            success: function (data) { alert("success!"); },
            error: function (request, type, errorThrown) {
                alert("request: " + request.status + 
                      "\nrequestText: " + request.statusText +
                      "\nType: " + type + 
                      "\nException: " + errorThrown); }
        });
    });

what am i doing wrong?

Your best bet is to pass the login credentials into your "login.php" (if its on the same server as your ajax call) and then use PHP's cURL function to pass the posted variables to your second domain's "login2.php".

I use this gateway / passthrough technique ALOT and it works for getting around those pesky cross-domain-policy issues.

I bump into this problem also, the thing is that the purpose of the Ajax call is to work on a single server for security reasons, so when you are sending the call to the other server you won't receive an answer, therefore the "success" method won't work. This doesn't mean that server1 didn't receive the data though, but you can't validate if it was correct.

An alternate solution would be, if you have the login information on a database on server1, you can use php to connect to the database and grant access to your origin server, and then you can make all your ajax calls base on that database connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM