【问题标题】:Unable to parse knockout JSON in PHP $_get无法在 PHP $_get 中解析淘汰赛 JSON
【发布时间】:2013-06-07 07:40:22
【问题描述】:

这是我的 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 上进行测试

【问题讨论】:

  • 如何将var_dump($_GET); 添加到您的 test.php 脚本中并首先检查其中的内容?

标签: php jquery knockout.js json


【解决方案1】:

我想你正在寻找:

data: {courses : ko.toJSON(self.courses)},

此外,如果您发送 JSON,POST 请求会比 GET 更好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2012-06-06
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多