【问题标题】:What is the Angular Equivalent for Jquery Ajax?Jquery Ajax 的 Angular 等效项是什么?
【发布时间】:2015-04-26 00:40:16
【问题描述】:
$.ajax({
            type: "GET",
            url: "../Home/RightPanel",
            data:{"movie_name":$(".search_columns").val()},
            contentType: "application/json;charset=utf-8",
            success: function (res) {
               $("#right_panel").html(res);
            },
            error: function (xhr) {
                alert(xhr.responseText);
            }
        });

我尝试使用 $http 而不是 $.ajax。但它没有用。

【问题讨论】:

  • 在你的控制器或服务中注入 $http。然后尝试 $http.get()

标签: ajax angularjs


【解决方案1】:

docs有一整节介绍如何使用它:

$http 服务是一个核心 Angular 服务,它通过浏览器的 XMLHttpRequest 对象或通过 JSONP 促进与远程 HTTP 服务器的通信。

$http 服务是一个函数,它接受一个参数——一个配置对象——用于生成一个 HTTP 请求并返回一个带有两个 $http 特定方法的承诺:成功错误

简单的 GET 请求示例:

$http.get('/someUrl').
    success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
    }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

简单的 POST 请求示例(传递数据):

$http.post('/someUrl', {msg:'hello word!'}).
    success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
    }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
  });

【讨论】:

    【解决方案2】:

    无法在 $http GET 请求中发送数据。您可以将数据作为查询字符串参数发送给您,也可以发出 POST 请求。 JQuery 自动转换查询字符串参数中数据中的值。

    带有查询字符串参数的GET请求:

    $http.get("../Home/RightPanel", 
    {
    params: { movie_name: $(".search_columns").val() }
    });
    

    您可以在AngularJS passing data to $http.get request 找到更多示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 2014-03-12
      • 2019-02-03
      • 2016-10-21
      • 2015-06-10
      • 2012-12-27
      相关资源
      最近更新 更多