【问题标题】:Send JSON data via POST PHP instead of Ajax version通过 POST PHP 而不是 Ajax 版本发送 JSON 数据
【发布时间】:2018-06-10 07:47:01
【问题描述】:

我必须创建一个 JSON 对象,它等效于使用此 Ajax 代码 sn-p 创建的 JSON 对象:

$.ajax({
    type    : "POST",
    url     : "?",
    dataType: "json",
    data    : "username=" + encodeURIComponent($('input[name="username"]').val()) + "&password=" + encodeURIComponent($('input[name="password"]').val()) + "&antiForgeryToken=d98188e0f56bafa75180591e38d189ee",
    success : function(json) {
        if(json.status == 'success') {
            window.parent.location.href = json.returnUrl;
            window.parent.$.fancybox.close();
        } else {
            $('#slfErrorAlert').show();
            $('.spispinner').hide();
            $maindiv.removeAttr("disabled");
            $maindiv.removeClass("instaclass31");
        }
    }
});
});

我只能使用 PHP,这是我目前使用来自 SimpleTestSimpleBrowser 库生成的:

$browser = new SimpleBrowser();
$str = '?username=aaa&password=bbb&antiForgeryToken=d98188e0f56bafa75180591e38d189ee';
$json = json_encode($str);
$browser->post($httpsPage, $json);

我指定我不必使用这个库及其方法,但例如我知道这也可以使用cURL 来完成,虽然我不知道如何。
当我启动脚本时,我会从我认为的 POST 服务器中得到一个通用的 {"status":"error"} 作为输出。
我错过了什么?

根据@Andy 使用json_encode 的正确语法:(仍然是相同的错误消息):

$data = array('username' => 'aaa', 'password' => 'bbb');
$json = json_encode($data);

【问题讨论】:

  • 接收脚本期望什么? $_POST 中的值是 php://input 中的值?
  • @Andy 我没写,但我认为它接收所有类型的请求,因为在 html 源代码中按钮不是 type="submit"

标签: php json ajax curl


【解决方案1】:

根据json_encodePHP 文档页面,您必须绕过数组进入此方法。您可以在这里找到更多详细信息:http://php.net/manual/en/function.json-encode.php

在你的情况下,你可能需要构建这样的东西:

$reqData = array('user' => 'foo', 'eamil' => 'bar@baz');
// {"user":"foo","email":"bar@baz"}
json_encode($reData);

【讨论】:

  • 我仍然收到错误消息 '{"status":"error"}'
  • 你能试试这个吗:$browser->post($httpsPage, $data);
  • 我在 Chrome 中尝试了你的 JS 并检查了请求。这是我发现的:image.prntscr.com/image/8YWdLzBBSP6Eo4MpAvD6gQ.png
  • 看起来内容类型必须是content-type:application/x-www-form-urlencoded; charset=UTF-8
  • 那么它应该是什么意思..?
【解决方案2】:

尝试使用 curl:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $httpsPage);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username='.rawurlencode('aaa').'&password='.rawurlencode('bbb').'&antiForgeryToken=d98188e0f56bafa75180591e38d189ee');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$server_output = curl_exec ($ch); 
curl_close ($ch);
print_r($server_output);

或者尝试使用您的代码而不编码为 json 后的 vars:

$browser = new SimpleBrowser();
$str = '?username='.rawurlencode('aaa').'&password='.rawurlencode('bbb').'&antiForgeryToken=d98188e0f56bafa75180591e38d189ee';
$browser->post($httpsPage, $str);

响应将采用 json 格式,但我认为不应该是 post vars。来自jQuery .ajax help:dataType(默认:Intelligent Guess(xml、json、script 或 html))您期望从服务器返回的数据类型。

【讨论】:

  • 但是作为 JSON,而不是查询格式。并且可能带有Content-type 标头,谁知道呢。
  • 这里的页面地址要放在哪里?
  • 在 jQuery 示例中,我知道发布数据不是 json,respose 将是 json 但不是 post vars。
  • 在注意到您的代码中定义了 $httpsPage 变量后,我更新了代码。
  • 嗯..没有json对象作为参数?我不知道 ajax,但当我读到“dataType=json”时,我假设了。但是,我已经尝试过该解决方案,但仍然无法正常工作。简而言之,“urlencode”是什么意思?
猜你喜欢
  • 1970-01-01
  • 2018-03-08
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
相关资源
最近更新 更多