javascript - Angular js $http post 不起作用

标签 javascript jquery angularjs http-post

我正在使用 http post 使用以下代码从网络服务获取数据..

$http({
                method: "post",
                url: "SurveyQuestions.asmx/GetSurveyQuestions",
                data: JSON.stringify({"empl_id" : '12345' , "Year" : '2015' }),
                headers: { 'Content-Type': 'application/json' },
                dataType: "json",
                contentType: "application/json; charset=utf-8",
            }).success(function (response) { //do something})

上面的代码使用不带参数的“get”可以正常工作。

我的帖子网络服务是

<WebMethod()> _
        <ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
        Public Sub GetSurveyQuestions(ByVal Empl_id as string, ByVal Year as string)
        Try
        //get list of records
          Dim js As JavaScriptSerializer = New JavaScriptSerializer

          Context.Response.Write(js.Serialize(listSurveyQuestions))
        Catch ex As Exception

      End Try

    End Sub

Web 服务执行正常并将数据返回到方法,但 $http 中的响应为空并出错。

最佳答案

你只是尝试传递仅在 url 最后添加的对象。

这是我的情况,我只是添加员工 ID。如果您仅发送员工对象,它也会正确。

$scope.removeEmployee = function(employee) {

var method = 'DELETE';

var url = 'http://localhost:8080/AngularJSWithRestful/rest/product/Delete/' + employee.id  ;

$http({

    method : method,
    url : url,

}).then(function successCallback(response) {

    $scope.product = response.data.product;

    }, function errorCallback(response) {

    });
};

关于javascript - Angular js $http post 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388093/

相关文章:

jquery - 基于 ajax 表单的 wmd 编辑器(jquery 版本)

node.js - Sails.js 应用程序在启动后不从 Assets 中刷新文件

javascript - Rx combineLatest 等待两个 observable 发出

javascript - 代码在 JSFiddle 中工作,但在浏览器中不起作用

php - 如何将 JavaScript 数组传递给 onclick ="(' ')"?

php - 当ajax仍在运行/锁定页面时加载另一个页面?

javascript - Jasmine ,Angular "rootScope.$broadcast"测试

angularjs - 如何使用 $q.all 的 AngularJS 哈希选项?

javascript - 我的指令没有触发

javascript - jQuery;在 .load() 之后定位 HTML