开发者

Jquery Cross Domain AJAX Communication with PHP

开发者 https://www.devze.com 2023-01-28 20:24 出处:网络
I\'m trying to create a simple login system that will have theforms 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

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 开发者_如何学Pythonfor="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


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()"}
    ]
  }
}}



                });


You can't do cross-domain ajax due to same origin policy.

You can do jsonp cross domain. Her's how with JQuery: docs.


you should use datatype jsonp

$.ajax({
            url: url, 
            async: true, 
            type: "GET", 
            dataType: "jsonp", 
            success: function(json){
                // handle success response here
            },
            error: function(){ alert("x.err"); }
        });
0

精彩评论

暂无评论...
验证码 换一张
取 消