【问题标题】:Receive Xamarin JSON Object to PHP将 Xamarin JSON 对象接收到 PHP
【发布时间】:2018-08-18 14:09:52
【问题描述】:

我想使用 HTTP Web POST 将数据从我的表单插入到我的服务器。我的代码在下面,我无法获取 JObject json 的值并将其发送到我的 php 代码中。

var caf = entCafNo.Text;
string url = "http://192.168.120.9:7777/TBS/mobile-request.php?Host=" + Constants.hostname + "&Database=" + Constants.database + "&Request=SendCaf";
string contentType = "application/json";
JObject json = new JObject
{
  { "CAF", caf }
};

HttpClient client = new HttpClient();
var response = await client.PostAsync(url, new StringContent(json.ToString(), Encoding.UTF8, contentType));

PHP 代码:

$request = $_GET["Request"];

if($request == "SendCaf"){
    $caf = $_POST["CAF"];

    $sql = "INSERT INTO tblCaf(CAFNo) 
            VALUES('$caf)";
    mysqli_query ($conn, $sql);
}

【问题讨论】:

  • 您的脚本对SQL Injection Attack 甚至if you are escaping inputs, its not safe! 都是开放的,在MYSQLI_PDO API 中使用prepared parameterized statements
  • 尝试添加print_r($_POST); print_r($_GET); 以查看发送到 PHP 的内容
  • @RiggsFolly 我的 php 代码没有获取数据
  • @RiggsFolly 我修复了我的链接我忘记了链接中的 php 文件我如何获取数据{ "CAF", caf }, { "CAF2", caf2 }
  • 有了这个错误,我猜(不知道 xamarin)该 URL 是错误的

标签: php json xamarin xamarin.forms httpclient


【解决方案1】:

使用此代码:

 $json_str = file_get_contents('php://input');
    $json_obj = json_decode($json_str);
    $caf = $json_obj->CAF;

    $sql = "INSERT INTO tblCaf(CAFNo) 
            VALUES('$caf')";
    mysqli_query ($conn, $sql);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2012-04-30
    • 1970-01-01
    • 2015-02-08
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多