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 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类