如何使用Angular解决这个问题,它在JQuery中完美运行?

时间:2016-11-02 01:13:01

标签: javascript jquery angularjs web-services

嗨,我是ANgularJs的新手。 我有一个WebService.asmx,它需要两个参数并返回一个boA变量,现在在jQuery中的MachineAssigned变量它工作得很好但不在Angulajs “我有一个完美运行的jQuery代码”

$('#saveButton').click(function () {
            var machineNo = $('#machineNumberTextBox').val();
            var empCode = $('#employeeCodeTextBox').val();
            var empName = $('#empNameTextbox').val();
            if (machineNo.length > 0 && empCode.length > 0) {
                $.ajax({
                    url: "WebService.asmx/IsMachineAssigned",
                    method: 'post',
                    data: 'machineNumber=' + machineNo + '&employeeCode=' + empCode,
                    dataType: 'json',
                    success: function (data) {
                        if (data.MachineAssigned) {
                            $('#divResult').text("Machine " + machineNo + " is already assigned to this " + empCode + " : " + empName);
                            $('#divResult').css('color', 'red');
                        } else {
                            $('#divResult').text("Machine " + machineNo + " is available for this " + empCode + " : " + empName);
                            $('#divResult').css('color', 'green');
                        }
                    }
                })
            }
        })

“但在AngularJS中,相同的逻辑不起作用”

var machineNo = $scope.machine.MachineNumber;
        var empCode = $scope.operator.EmployeeCode;
        //var params = { machineNumber: machineNo, employeeCode: empCode }
        $http({
            method: 'post',
            url: "WebService.asmx/IsMachineAssigned",
            //date: 'machineNumber=' + JSON.parse(machineNo) + '&employeeCode=' + JSON.parse(empCode),
            contentType: "application/json; charset=utf-8",
            data: 'machineNumber=' + machineNo + '&employeeCode=' + empCode,
            datatype: "json"
        }).success(function (resopnse) {
            if (data.MachineAssigned) {
                alert("This Machine already assigned");
            } else {
                alert("This Machine is available");
            }
        })

我试过每件事请帮帮我

1 个答案:

答案 0 :(得分:1)

试试这个:

var machineNo = $scope.machine.MachineNumber;
var empCode = $scope.operator.EmployeeCode;
var params = $.param({ 'machineNumber': machineNo, 'employeeCode': empCode });
$http({
        method: 'POST',
        url: 'MultifabsWebService.asmx/IsMachineAssigned',
        data: params,
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function (response) { // success function
            console.log(response);
            if (response.data.MachineAssigned) {
                alert("This Machine already assigned");
            } else {
                alert("This Machine is available");
            }
        },
        function (response) { // error function
            console.log(response);
        });