通过Javascript调用获取php上的Ajax POST数据 [英] Get Ajax POST data on php via Javascript call

查看:46
本文介绍了通过Javascript调用获取php上的Ajax POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我认为我新手 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 代码连接数据库

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框架。因此,当我发布数据时无法调试它。正常模式我能够调试我的代码。但不是来自remote.for我在 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

For, data:JSON.stringify(ko.toJS(params)),

For, data: JSON.stringify(ko.toJS(params)),

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


推荐答案

如果我理解正确,你需要创建一个能够从客户端接收类似REST的请求的PHP服务。

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

为了做thad,你需要访问原始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文档(最重要的选项是 data processData )。

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).

这篇关于通过Javascript调用获取php上的Ajax POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆