CashLee李秉骏
CashLee李秉骏

Reputation: 1048

How to use ajax to across domain?

I post my code here.(I add the jquery into my web page)

var dataString ='username='+ username + '&password=' + password; 
//alert (dataString);return false; 

$.ajax({ 
        type: "POST", 
        url: "http://hisencha.sinaapp.com/login.php", 
        data: dataString, 
        success: function(data) { 

          if(data=='success'){
            window.location.href='list.html';
          }
          else{
            alert(data);
          };
        } 
      }); 

above is part of javascript

here is php(demo)

<?php
if ($_POST['username']=='aaa' && $_POST['password']=='aaa')
{
  echo 'success';
}
else
{
  echo 'error';
}
?>

What can I do now?And how to fix the error about acrossing domain.

Thank you thank you!

Upvotes: 0

Views: 165

Answers (4)

skimberk1
skimberk1

Reputation: 2094

Yeah, you can use JSONP to do it. Here's some sample jQuery:

 $.ajax({ 
     type: "POST", 
     url: "http://hisencha.sinaapp.com/login.php?callback=?", 
     data: dataString,
      dataType: 'JSONP',
     success: function(data) { 

       if(data=='success'){
         window.location.href='list.html';
       }
       else{
         alert(data);
       };
     } 
   });

And PHP:

<?php
$response = array(
     'something' => 'something'
);
echo $_GET[['callback'].'('.json_encode($response).')';
?>

Upvotes: 1

Arif RH
Arif RH

Reputation: 175

There are many example to do it, with AJAX-CROSS-DOMAIN or Proxy Handle.

Upvotes: 0

Haim Evgi
Haim Evgi

Reputation: 125564

you can do it with JSONP. an example

Upvotes: 0

Moe Sweet
Moe Sweet

Reputation: 3721

There's no way to use ajax across domains. It's blocked for security reasons.

Upvotes: 0

Related Questions