dtl4521
dtl4521
2012-09-18 21:51
浏览 89

ajax jquery php提交

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 + 
                      "
requestText: " + request.statusText +
                      "
Type: " + type + 
                      "
Exception: " + errorThrown); }
        });
    });

what am i doing wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doty58493
    doty58493 2012-09-18 22:22

    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.

    点赞 评论
  • dqzg62440
    dqzg62440 2012-09-18 22:28

    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.

    点赞 评论

相关推荐