这是我的 JavaScript 代码
var student = student || {};
student.viewModel = function () {
var self = this;
self.courseCode = ko.observable("");
self.courses = ko.observableArray([
{ "code": "MTH101", "title": "General Mathematics I" },
{ "code": "CHM101", "title": "Introductory Chemistry I" },
{ "code": "PHY101", "title": "General Physics I" },
{ "code": "BIO101", "title": "Introduction to Biology" }
]);
self.removeCourse = function () { self.courses.remove(this); };
self.addCourse = function (data) {
self.courses.push({ code: data, title: "A new Course added " + new Date() });
$('#courseModal').modal('hide');
};
self.save = function() {
$.ajax("test.php", {
data: ko.toJSON({ courses: self.courses }),
type: "get", contentType: "application/json",
success: function(result) { alert(result) }
});
};
};
student.VM = new student.viewModel();
$(document).ready(function () {
ko.applyBindings(student.VM);
});
如何读取 PHP 中发送的数据? json_decode()
只接受字符串,当我也使用时,htmlspecialchars($_GET["courses"])
我得到错误未定义的索引课程。我只想告诉用户发送到服务器的课程数量。
如果有帮助,我正在使用 wamp 在 localhost 上进行测试