2015-12-02 55 views
0

嗯,我已經研究了角度js文檔,並且此代碼在同一臺服務器上完美工作;不知何故,不在跨站點工作;本地主機可到達如何在angular js中使用ajax調用和使用RestFul webservice?

module='user'; 
siteurl='http://localhost/angularjs'; 
url=siteurl+"/admin/"+module+"/list"; 
BlockUi(); 
$http({ 
     url  : url, 
     method : "post", 
     data : "", 
     headers : {'Content-Type':undefined} 

    }).then(function(responseText){ 
     $scope.totaltablerows=responseText.data.response_data; 
     $scope.tablerows=$scope.totaltablerows; 
     UnblockUi(); 
     $scope.searchFunction(); 

    },function(error){ 
     alert(JSON.stringify(error)); 
     UnblockUi(); 
     UnknownError(); 
    }); 

我應該怎麼做,以便這可以工作。

+0

也許你在localhost之後缺少端口? – Vivek

+0

我試過了嗎?謝謝你的迴應 –

回答

1

如果你的本地主機是你的後端還運行,沒有必要明確提到這一點,所以你的代碼看起來像:

BlockUi(); 
$http({ 
     url  : "/admin/user/list", 
     method : "post", 
     data : "", 
     headers : {'Content-Type': 'application/json'} 

    }).then(function(responseText){ 
     $scope.totaltablerows=responseText.data.response_data; 
     $scope.tablerows=$scope.totaltablerows; 
     UnblockUi(); 
     $scope.searchFunction(); 

    },function(error){ 
     alert(JSON.stringify(error)); 
     UnblockUi(); 
     UnknownError(); 
    }); 

您不必提'http://localhost/angularjs'之初網址,還有,爲什麼你將content-type設置爲undefined

+0

是的,但你可以從服務器外面調用它;那麼,以及content-type:undefined意味着你可以傳遞可能或不可以使用json格式的數據 –

相關問題