In host1 with domain http://domain1 I am using ajax with structure:
- index.php (that is include ajax)
- config.php (connect database)
- get_city.php
code here:
index.php
$(document).ready(function(){
$('.nation').change(function(){
var id = $(this).val();
var dataString = 'id='+id;
$.ajax({
type: 'POST',
url: '**http://domain1/get_data.php**',
data: dataString,
cache: false,
success: function(html) {
$('.city').html(html);
}
});
});
});
in get_city.php:
<?php
include 'config.php';
$id = $_POST['id'];
if($id) {
$query = mysql_query("Select * From jos_city Where nation_id = id");
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['city_name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
}
When I use the other host with domain http://domain2, then ajax can’t load
$(document).ready(function(){
$('.nation').change(function(){
var id = $(this).val();
var dataString = 'id='+id;
$.ajax({
type: 'POST',
url: '**http://domain1/get_data.php**',
data: dataString,
cache: false,
success: function(html) {
$('.city').html(html);
}
});
});
});
I think, the error occurs when calling the url: ‘http://domain1/get_data.php‘ from domain1 to domain2. Has anybody got an idea, why this might happen?
AJAX calls must conform to same origin policy, you cannot make plain AJAX calls to another domain.
There are, however, workarounds: