douershuang7356 2010-12-06 04:32
浏览 364
已采纳

Jquery与PHP的跨域AJAX通信

I'm trying to create a simple login system that will have the forms and everything on one domain (Let's call it Domain A) and the PHP files on another domain (Domain B). Why? Simply because Domain A is more reliable but doesn't support PHP. I'm quite new to all this AJAX stuff, so I'm currently using the resources I've found online.

The Jquery and Forms part of it on Domain A are as below:

<form id="submit" method="post">
<fieldset>
<legend>Enter Information</legend>  

<label for="fname">Name:</label>  
<input class="text" id="fname" name="fname" size="20" type="text" />  

<label for="lname">Email:</label>  
<input class="text" id="lname" name="lname" size="20" type="text" />  

<button class="button positive"> Submit </button>  </fieldset>
</form>
<script>
$(document).ready(function(){  
    $("form#submit").submit(function() {  
    // we want to store the values from the form input box, then send via ajax below  
    var fname     = $('#fname').attr('value');  
    var lname     = $('#lname').attr('value');  
        $.ajax({  
            type: "POST",  
            url: "http://domainB.com/somefolder/ajax.php",  
            data: "fname="+ fname +"& lname="+ lname,  
            success: function(){  
                $('form#submit').hide(function(){$('div.success').fadeIn();});  

            }  
        });  
    return false;  
    });  
});  
</script>

Please note that the 'lname' and 'fname' are going to represent Name and Email instead of Firstname and Lastname respectively.

The PHP code on Domain B :

<?php  

    // CLIENT INFORMATION  
    $uname        = htmlspecialchars(trim($_POST['fname']));  
    $umail        = htmlspecialchars(trim($_POST['lname']));  
  $uip = 12345;
  $usecret = "secret";
//Mysql
$con = mysql_connect("mysql.domainB.com","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
//Connect
mysql_select_db("login_db", $con);
//Insert user details irrespective of whether the user is a member or not to make a log kind of thing
mysql_query("INSERT INTO log_data (Name, Email, IP_ADDRESS, Secret)
VALUES ('$uname', '$umail', '$uip', '$usecret')");

//Now check if the user is a subscriber or not


//   mysql_query("SELECT Email, Secret FROM login_data WHERE Email='$umail' AND Secret="secret"");
//I don't know the code to check if the user has logged in correctly
//This is where I need you to help me
//I need to check if the user has logged in correctly, if yes, then return a message saying //success which should be caught by Jquery and displayed as an Alert, or something
//If the user login failed, this php code should return a failure message which again should be caught by Jquery on Domain A and displayed as an alert in the least.


mysql_close($con);
//end Mysql

?>  

Things to note here - There are two tables inside the login_db database - log_data which stores the log information and the login_data which stores usernames and passwords. The variable $secret stores the user's password.

The log_data insertion works like a charm, but its the login_data that is bugging me. Any help would be much appreciated. Thanks!

Imag

  • 写回答

3条回答 默认 最新

  • doudao2954 2010-12-06 05:12
    关注

    Just give a trying the below code...it should take care of cross domain issue also

    var dataString = "fName=" + $('#fname').attr('value') + "&lname=" + $('#lname').attr('value');
                    var url = "http://demo/exampe.php?" + dataString;
    
    
                  $.getJSON(url + "&jsoncallback=?", function(data){
    
                    alert("success");
    

    jsonp object construction should be as below..

    {"menu": {
      "id": "file",
      "value": "File",
      "popup": {
        "menuitem": [
          {"value": "New", "onclick": "CreateNewDoc()"},
          {"value": "Open", "onclick": "OpenDoc()"},
          {"value": "Close", "onclick": "CloseDoc()"}
        ]
      }
    }}
    
    
    
                    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?