简体   繁体   English

将数据从knockout发送到php

[英]Sending data from knockout to php

I created a login function 我创建了一个登录功能

self.login = function() {
    var credentials = {
        email: self.email(),
        pass: self.pass()
    }
    var data = ko.toJS(credentials);
    $.ajax({
        url: 'client/scripts/pages/login/login.php',
        type: 'post',
        data: {data: data},
        contentType: 'application/json',
        success: function (result) {
            alert(result);
        }
    });
}

This will send request to php file when the form is submitted. 这将在提交表单时向php文件发送请求。 Here is the login.php code, 这是login.php代码,

$data = json_decode($_POST['data']);
return $data['email'];

When i execute this, the result is Undefined index: data 当我执行此操作时,结果是Undefined index:data

I tried JSON.strignify but it is not working. 我尝试过JSON.strignify,但它没有用。 how can I get the email of the user in php? 如何在php中获取用户的电子邮件?

I'm using the following code: 我正在使用以下代码:

Client-side (JS): 客户端(JS):

$.ajax((<any>{
    type: "POST",
    async: async,
    url: dataServiceBaseUrl + "request.php", //
    data: {
        "jsonrpc": "2.0",
        "method": method,
        "dbname": dbName,
        "params": JSON.stringify(params)
    },
    success: function(data) {
        var obj = JSON.parse(data);
    },
    error: function() {
    }
}));

Server-side (PHP): 服务器端(PHP):

function process_request() {
    $dbname = $_POST["dbname"];
    $method = $_POST["method"];
    $params = json_decode($_POST["params"], true);
    $result = call_user_func_array("methods::" .  $method, $params);
    return json_encode(array("jsonrpc" => "2.0", "result" => $result, "error" => null, "usename"=>$usename));
}

Of course, 当然,

var data = ko.toJS(credentials);

is necessary to get serializable data to sent. 需要获取可发送的可序列化数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM