net :: ERR_CONNECTION_REFUSED尝试从JS调用GET请求到Slim PHP时

时间:2015-07-08 17:50:40

标签: javascript php ajax

我正在尝试将PHP Slim集成到我的HTML项目中。为此我在slim v1 / index.php中有一个代码为

<body>
<expando>The quick brown</expando> fox jumps over <expando>the lazy dog</expando>.
<script src="loadExpandos.js"></script>
</body>

在我的HTML中我有

$app->get('/albums', function() {
global $user_id;
$response = array();
$db = new DbHandler();
// fetching all user tasks
$result = $db->getAllAlbums();
$response["error"] = false;
$response["tasks"] = array();

// looping through result and preparing tasks array
while ($task = $result->fetch_assoc()) {
$tmp = array();
$tmp["id"] = $task["id"];
$tmp["album_year"] = $task["album_year"];
$tmp["album_img"] = $task["album_img"];
$tmp["music_director"] = $task["music_director"];
$tmp["album_desc"] = $task["album_desc"];
array_push($response["tasks"], $tmp);
}
echoRespnse(200, $response);
 });

function echoRespnse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);

// setting response content type to json
$app->contentType('application/json');

echo json_encode($response);
}

ajaxGetPost.js有

<script src="js/jquery.min.js"></script>
<script src="js/ajaxGetPost.js"></script>
<script>


$(document).ready(function()
{
var base_url="http://localhost/v1/";
var url,encodedata;
$("#update").focus();

/* Load Updates */
url=base_url+'albums';
encodedata='';
ajax_data('GET',url, function(data)
{
$.each(data.updates, function(i,data)
{

    alert("I am an alert box!");
var html="<div class='span5'><img class='four-radius' src='"+data.album_img+"'></div>";

$(html).appendTo("#mainContent");
});

});
});
</script>

当我运行时,我在控制台中出现以下错误

function ajax_data(type,url, success)
{
$.ajax({
type:type,
url:url,
dataType:"json",
contentType: 'application/json',
restful:true,
cache:false,
timeout:20000,
async:true,
beforeSend :function(data) { },
success:function(data){
success.call(this, data);
},
error:function(data){
alert(data);
}
});
}

我不确定它有什么问题。我试着调试脚本。因此在错误中添加了警报数据。我在警报中得到[object Object]。

调用API时,我得到以下用途

GET http://localhost/adhandapani/v1/albums net::ERR_CONNECTION_REFUSEDx.ajaxTransport.send @ jquery.min.js:2992x.extend.ajax @ jquery.min.js:2827ajax_data @ ajaxGetPost.js:46(anonymous function) @ index.php:42x.Callbacks.c @ jquery.min.js:1075x.Callbacks.p.fireWith @ jquery.min.js:1119x.extend.ready @ jquery.min.js:130q @ jquery.min.js:39

1 个答案:

答案 0 :(得分:0)

在我的本地主机上,我实际上是在端口8888上运行而我没有指定它..这让我犯了错误