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

时间:2022-10-18 00:27:41

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.

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

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

客户端:我使用的是knockout js(Javascript)。调用PHP服务。

My Code:

我的代码:

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);
   }
}

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

服务器端我的代码,我正在使用这个PHP代码来连接DB。

PHP Code:

PHP代码:

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

*My Question *:

*我的问题*:

  • I am not clear on how to POST data in PHP ? I tried with $_POST[''] method as well as many more.
  • 我不清楚如何在PHP中发布数据?我尝试了$_POST["]方法以及更多的方法。
  • 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.
  • 我使用eclipse作为php框架。因此,当我发布数据时,无法调试它。通常情况下,我可以调试我的代码。但不是从远程。为此,我对php进行了修改。ini文件。

How to get Response of Post Data on php code ?

How to debug via remote post ?

My Request sample:

我请求示例:

suppose i use:

假设我使用:

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

对于数据:params,只有在那个时候我的请求格式是。

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

的数据:JSON.stringify(ko.toJS(params)),

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

4 个解决方案

#1


7  

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

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

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

为了实现这个目的,您需要访问原始POST数据。在PHP中,它是这样做的:

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

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

您可以在wrappers文档中阅读更多关于php://的输入。

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.

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

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

如果您传递一个对象,它将通过jQuery使用查询字符串格式在内部转换为字符串。关于这一点,请参见jQuery文档中的$。ajax(最重要的nt选项是数据和处理数据)。

#2


3  

Just pass the ajax data param as an object, don't convert it into JSON. Then in PHP use $_POST directly.

只需将ajax数据param作为对象传递,不要将其转换为JSON。然后在PHP中直接使用$_POST。

Use firebug or chrome dev tools to analyze the ajax request and see which data is sent

使用firebug或chrome开发工具分析ajax请求并查看发送了哪些数据

#3


-1  

Use this simple jquery function to accomplish your task

使用这个简单的jquery函数来完成任务

$.ajax({           
            type: "POST",  
            url:"scripts/dummy.php",  
            data:"tbl="+table,
            dataType:"json", //if you want to get back response in json

            beforeSend: function()
            {                   

            },
            success: function(resp)
            {               

            }, 

            complete: function()
            {

            },

            error: function(e)
            {  
            alert('Error: ' + e);  
            }  

    }); //end Ajax

#4


-1  

in PHP use:

在PHP中使用:

if(isset($_POST['ClientData'])){
   $client_data = $_POST['ClientData']
}

now $client_data variable should contain the array.

现在$client_data变量应该包含数组。

For debugging purpose you can use php's built-in print_r() function. It's pretty handy.

为了调试目的,可以使用php的内置print_r()函数。它很方便。

here's is an example:

这里是一个例子:

//make sure it's post request
if(isset($_POST)){
   //now print the array nicely
   echo "<pre>";
   print_r($_POST);
   echo "</pre>";
}

#1


7  

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

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

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

为了实现这个目的,您需要访问原始POST数据。在PHP中,它是这样做的:

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

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

您可以在wrappers文档中阅读更多关于php://的输入。

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.

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

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

如果您传递一个对象,它将通过jQuery使用查询字符串格式在内部转换为字符串。关于这一点,请参见jQuery文档中的$。ajax(最重要的nt选项是数据和处理数据)。

#2


3  

Just pass the ajax data param as an object, don't convert it into JSON. Then in PHP use $_POST directly.

只需将ajax数据param作为对象传递,不要将其转换为JSON。然后在PHP中直接使用$_POST。

Use firebug or chrome dev tools to analyze the ajax request and see which data is sent

使用firebug或chrome开发工具分析ajax请求并查看发送了哪些数据

#3


-1  

Use this simple jquery function to accomplish your task

使用这个简单的jquery函数来完成任务

$.ajax({           
            type: "POST",  
            url:"scripts/dummy.php",  
            data:"tbl="+table,
            dataType:"json", //if you want to get back response in json

            beforeSend: function()
            {                   

            },
            success: function(resp)
            {               

            }, 

            complete: function()
            {

            },

            error: function(e)
            {  
            alert('Error: ' + e);  
            }  

    }); //end Ajax

#4


-1  

in PHP use:

在PHP中使用:

if(isset($_POST['ClientData'])){
   $client_data = $_POST['ClientData']
}

now $client_data variable should contain the array.

现在$client_data变量应该包含数组。

For debugging purpose you can use php's built-in print_r() function. It's pretty handy.

为了调试目的,可以使用php的内置print_r()函数。它很方便。

here's is an example:

这里是一个例子:

//make sure it's post request
if(isset($_POST)){
   //now print the array nicely
   echo "<pre>";
   print_r($_POST);
   echo "</pre>";
}