【问题标题】:How to have ajax call and use of RestFul webservice in angular js?如何在 Angular js 中进行 ajax 调用和使用 RestFul webservice?
【发布时间】:2016-03-06 08:20:19
【问题描述】:

好吧,我研究了 Angular js 文档,这段代码在同一台服务器上运行完美;不知何故不能在跨站点上工作;本地主机可达

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();
    });

我应该怎么做才能使它起作用。

【问题讨论】:

  • 也许您在 localhost 之后缺少端口?
  • 我试过了吗?感谢您的回复

标签: angularjs ajax cross-domain


【解决方案1】:

如果您的 localhost 也是您的后端运行的地方,则无需明确提及,因此您的代码如下所示:

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();
    });

你不需要在URL开头提到'http://localhost/angularjs',还有,你为什么将content-type设置为undefined

【讨论】:

  • 是的,但是您可以从服务器外调用它;那怎么样,content-type:undefined 意味着你可以传递可能是也可能不是 json 格式的数据
猜你喜欢
  • 2016-05-04
  • 1970-01-01
  • 2015-04-30
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
相关资源
最近更新 更多