这是我的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"])
时,我收到错误未定义的索引课程。< / em>我只想告诉用户发送到服务器的课程数量。
如果有帮助,我正在使用wamp
在localhost上进行测试答案 0 :(得分:1)
我认为你在寻找:
data: {courses : ko.toJSON(self.courses)},
另外,如果您发送JSON,则POST请求将优于GET。