jQuery AJAX是否可以在不同的服务器上运行?

时间:2014-03-30 15:20:51

标签: javascript php jquery ajax

当我在我的笔记本电脑上使用我的本地主机(MAMP)时,请求工作得很好但是当我将我的Web应用程序移动到另一台服务器时,它似乎无法返回error: function() { console.log("oh no");。代码如下:

var backendURL = "http://dayout.uphero.com/dayout-app/php/";

.factory('AttractionService', function() {
     // Returns a JSON array

     var attractions;

     return {
     all: function() {

     jQuery.ajax( backendURL + "attractions_summary.php", {
                 async: false,
                 cache: false,
                 error: function() { console.log("oh no"); },
                 success: function(oData) {
                 attractions = oData;
                 }
                 });

     return attractions;
     }
     }
     })

这是与服务器连接并返回JSON数组的attractions_summary.php文件:

<?php

 include "../../config.php";

 header('Content-type: application/json');
 header('Access-Control-Allow-Origin: *');

 $summary = mysql_query ('SELECT attraction.attraction_name AS NAME, attraction.image_name AS IMAGE, 
 attraction.attraction_price AS PRICE, attraction.id AS ID, bus.b_name AS BUSINESS FROM Attractions AS attraction 
 INNER JOIN BusinessInfo AS bus ON attraction.user_id = bus.user_id ORDER BY attraction.id DESC');

$result_formatted = array();
$i = 0;

while($result = mysql_fetch_array($summary)) {
$result_formatted[$i] = array();
$result_formatted[$i]['id'] = mysql_real_escape_string($result["ID"]);
$result_formatted[$i]['attraction_name'] = mysql_real_escape_string($result["NAME"]);
$result_formatted[$i]['attraction_price'] = mysql_real_escape_string($result["PRICE"]);
$result_formatted[$i]['bname'] = mysql_real_escape_string($result["BUSINESS"]);
$result_formatted[$i]['image_name'] = mysql_real_escape_string($result["IMAGE"]);
$i++;
}

echo json_encode($result_formatted);

?>

1 个答案:

答案 0 :(得分:1)

jQuery Ajax是否可以在不同的服务器上运行? - 是的

[{"id":"4","attraction_name":"gerfwedq","attraction_price":"324","bname":"Texaco Garage","image_name":"icon-6013953941166125.png"},{"id":"1","attraction_name":"Starbucks Attraction Day!","attraction_price":"175.99","bname":"Starbucks","image_name":"footprints.png"}]
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

这是你网址的输出......

删除以下

<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

我认为这是通过自动托管添加的