嗨,我是 ANGularJ 的新手。 我有一个 WebService.asmx,它需要两个参数并在 MachineAssigned 变量中返回一个 bool 变量,现在在 jQuery 中它工作得很好,但在 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");
}
})
我尝试了一切,请帮助我
最佳答案
试试这个:
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);
});
关于javascript - 如何使用在 JQuery 中完美运行的 Angular 来解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40371033/