通过 Javascript 调用在 php 上获取 Ajax POST 数据

2021-12-29 00:00:00 php javascript sql-server knockout.js

首先我承认我是新手php,我在客户端使用 jquery(knockout js) &服务器端的 PHP.我的代码.

First I am conface that I am Newbie to php, I am using jquery(knockout js) at client side & PHP at server side. my code.

客户端:我正在使用 knockout js(Javascript).调用我的 PHP 服务.

Client side: I am using knockout js(Javascript). to call my PHP service.

我的代码:

self.VMSaveEditUserMode = function () {
   try {
      var params = { "ClientData": [controllerVM_.ClientID(), controllerVM_.VMList[0].ClientName(), controllerVM_.VMList[0].ShortName(), controllerVM_.VMList[0].Address(), controllerVM_.VMList[0].CreatedBy(), controllerVM_.VMList[0].CityName(), controllerVM_.VMList[0].PostalCode(), controllerVM_.VMList[0].ContactEmail(), controllerVM_.VMList[0].ContactPhone(), controllerVM_.VMList[0].IsCorporate()] };

      $.ajax({
         type: "POST",
         url: URL + "index.php/phpService/SaveClient/" + controllerVM_.TokenKey(),
         data: JSON.stringify(ko.toJS(params)),
         contentType: "application/json",
         async: true,
         dataType: 'json',
         cache: false,
         success: function (response) {
         },
         error: function (ErrorResponse) {
            if (ErrorResponse.statusText == "OK") {
            }
            else {
               alert("ErrorMsg:" + ErrorResponse.statusText);
            }
         }
      });
   }
   catch (error) {
      alert("Catch:" + error);
   }
}

服务器端我的代码,我使用这个PHP代码连接DB.

Server Side My Code, I am using this PHP code to connect with DB.

PHP 代码:

public function SaveClient($userToken)
    {   
       $value = json_decode($Clientdata);
       echo $value->ClientData[0];
    }

*我的问题*:

  • 我不清楚如何在 PHP 中发布数据?我尝试了 $_POST[''] 方法以及更多方法.
  • 我使用 eclipse 作为 php 框架.所以,当我发布数据时无法调试它.通常模式我可以调试我的代码.但不能从远程调试.为此我也对 php.ini 文件进行了更改.
  • I am not clear on how to POST data in PHP ? I tried with $_POST[''] method as well as many more.
  • I am using eclipse as a php framework. so, not able to debug it when i post the data.Normally mode i am able to debug my code.but not from remotely.for that i made changes on php.ini file also.

我的请求示例:

假设我使用:

for, data: params, 只是当时我的请求格式是.

For, data: params, only at that time my request format is.

ClientData%5B%5D=4&ClientData%5B%5D=kamlesh&ClientData%5B%5D=KAM&ClientData%5B%5D=Junagadh&ClientData%5B%5D=me&ClientData%5B%5D=SANTA+ROSA&ClientData%5B%5D=76220&ClientData%5B%5D=kamlesh.vadiyatar%40gmail.com&ClientData%5B%5D=9998305904&ClientData%5B%5D=false

对于,data: JSON.stringify(ko.toJS(params)),

{"ClientData":["4","kamlesh","KAM","Junagadh","me","SANTA ROSA","76220","kamlesh.vadiyatar@gmail.com","9998305904",false]}

推荐答案

如果我理解正确,您需要创建一个 PHP 服务,该服务能够接收来自客户端的类似 REST 的请求.

If I understand correctly you need to create a PHP service which is able to receive REST-like requests from client.

为了做到这一点,您需要访问原始 POST 数据.在 PHP 中它是这样完成的:

In order to do thad you need to access raw POST data. In PHP its being done like this:

$ClientData = file_get_contents('php://input');

您可以在 php://input 的更多信息>包装文档.

You can read more about php://input in the wrappers documentation.

当然,从客户端发送数据需要使用 POST 方法和原始数据,即作为字符串.您可以使用 JSON.stringify() 从对象中获取字符串,您已经这样做了.

Of course from the client's side the data need to be sent using the POST method and as raw data, i.e. as a string. You can obtain a string from object using JSON.stringify() which you already do.

如果您传递一个对象,它将使用查询字符串格式由 jQuery 在内部转换为字符串.更多关于 $.ajax 的 jQuery 文档(最重要的选项是 dataprocessData).

If you pass an object, it will be converted to string internally by jQuery using query-string format. More on that in the jQuery documentation for $.ajax (the most importatnt options being data and processData).

相关文章