php - $_POST 变量在 php Titanium 中不起作用

标签 php javascript android post titanium-mobile

我正在尝试在 JS 文件和 PHP 文件之间传递数据,但 PHP 中的变量 $_POST 不起作用,因此我无法在应用程序中进化!

JS代码:

var params = String(input.value);
    var xhr = Titanium.Network.createHTTPClient();
    xhr.open('GET','http://10.0.2.2/jobfinder/teste_demo_grafica/Resources/teste.php');
    xhr.send(params);
    xhr.onload = function(){

    var response = this.responseText;
    alert(response);
    if (response != null)
    {
        alert("voltou ao js e funca");
    }
    else
    {
        alert("-.-");
    }
    };

    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

    xhr.onerror = function(e){alert('Transmission error: ' + e.error);};

});

现在是 php 代码:

<?php

// Connect to the database(host, username, password)
$con = mysql_connect('localhost','root','');

if (!$con)
{
    return ("Failed to make connection.");
    exit;
}

// Select the database. Enter the name of your database (not the same as the table name)
$db = mysql_select_db('jobfinder');
if (!$db)
{
    echo "Failed to select db.";
    exit;
}
$pesquisa= $_POST[params];
echo "pesquisa";
$sql = "SELECT * FROM oferta WHERE titulo like '%$pesquisa%'";  
$query = mysql_query($sql); 

if (mysql_num_rows($query) > 0)
{

    $row = mysql_fetch_array($query);
    $response = array(
        'titulo' => $row['titulo'],
        'oferta' => $row['descricao_oferta']
    );
    json_encode($response);
    echo $response['titulo'];

}
else
{

    // Else the username and/or password was invalid! Create an array, json_encode it and echo it out
    $response =  array(
        'message' => 'Não existem ofertas para esta pesquisa'
    );
    json_encode($response);


}
echo "php mode off";
?>

最佳答案

据猜测,您的 JavaScript 正在通过 GET 发送数据:

xhr.open('GET','http://10.0.2.2/jobfinder/teste_demo_grafica/Resources/teste.php');

但是你的 PHP 正在寻找 POST。将 POST 更改为 GET 可能会起作用。反之亦然

关于php - $_POST 变量在 php Titanium 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10171202/

相关文章:

php - QuickBooks 在解析提供的 XML 文本流时发现错误

php - 当网站用户通过 php 下载文件时,如何创建数据库日志?

android - 将 flutter android 项目导入 iOS

Android 按钮按下强度

android - Android 是否支持 JDBC

PHP 函数 'return' 未返回

php - 想要在点击提交按钮后使用 AJAX 刷新内容

javascript - 如何在 Javascript 中使用键值查找数组中的最新日期

javascript - 按第 5 个字段对多维数组进行排序

javascript - 如果函数处于错误情况下,我该如何停止执行?