简体   繁体   English

从ajax请求发布或获取不存在的数据

[英]Post or Get data not present from ajax request

I want to validate the value entered in a text input field using an ajax request to the server however when I try to access the post or get data, they are both empty arrays. 我想验证使用对服务器的ajax请求在文本输入字段中输入的值,但是当我尝试访问帖子或获取数据时,它们都是空数组。

The html input: html输入:

<input type="text" name="club_key" id="club_key" class="form-control" onblur="verifyKey(this);">

The JavaScript: JavaScript:

function verifyKey(ev)
{
    var url = '/index.php?/orders/key';
    // url = encodeURIComponent(url);

    $.ajax({
        cache: false,
        url: url,
        type: "POST",
        data: ev.value
    }).done(function(response) {
        alert(response);
    });

The server side PHP: 服务器端PHP:

public function check_key()
{
    echo '<pre>';
    echo "Clubbbed to death: ";
    // print_r($this->input->get_post());
    print_r($_GET);
    print_r($_POST);
    echo '</pre>';
}

Both are empty arrays. 两者都是空数组。 There is no post or get data present. 没有发布或获取数据。 The site is a CodeIgniter site, so in the routing I have: 该站点是一个CodeIgniter站点,因此在路由中,我有:

$route['orders/key']    = "orders/check_key";

I have also tried: 我也尝试过:

$route['orders/key/(:any)'] = "orders/check_key/$1";

Any ideas why there's no post data? 有什么想法为什么没有帖子数据?

PHP expects form submissions (via get and post) to be in a key = value format. PHP希望表单提交(通过get和post)采用key = value格式。 Your data: ev.value is JUST a value: ev.value . 您的data: ev.value只是一个值: ev.value Since there's no key, PHP cannot put anything into $_GET or $_POST, because all array entries must have a key. 由于没有键,PHP无法将任何内容放入$ _GET或$ _POST,因为所有数组条目都必须具有键。

Try 尝试

data: {foo: ev.value}

and then 接着

$_POST['foo']

Alternatively, you could try 或者,您可以尝试

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

which will read that bare string directly from the input, but it'll be completely unprocessed by PHP. 它将直接从输入中读取裸字符串,但是PHP完全不会对其进行处理。

What happens if you use, instead 如果使用,会发生什么情况

$.ajax({
    cache: false,
    url: url,
    type: "POST",
    data: { club_key : jQuery('#club_key').val() }
}).done(function(response) {
    alert(response);
});

data element should be an object, not a scalar. 数据元素应该是一个对象,而不是标量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM