【问题标题】:jQuery Ajax with POST to send string doesn't work使用 POST 发送字符串的 jQuery Ajax 不起作用
【发布时间】:2015-04-17 16:02:00
【问题描述】:

我已经在这里看到了所有的问答,但我无法解决问题。 我正在尝试发送两个字符串。

var country_code = 'PT';
  $.ajax({
    url: 'GetData.php',
    type: 'POST',
    data: {'country': country_code},
    dataType: "json",
    success: function(res) {
      ...
    },
    error: function () {                    
      console.log("There was an error");
  }
});

在 GetData.php 上我只有

$cty = $_POST['country'];

$query = "SELECT DISTINCT uuid_client, country FROM users_insertions WHERE country='".$cty."'";
...
echo json_encode($res); 

但是,它不起作用。我无法获取 GetData.php 上的字符串 我做错了什么?

【问题讨论】:

  • 错误是什么?尝试使用die($_POST['country']) 并检查您的控制台。
  • 代码运行良好,问题出在哪里?
  • 我发现了问题,真的很奇怪。它与 URL 有关,因为我在 .htaccess 根文件中进行了一些更改:“应用程序配置不允许给定 URL:应用程序的设置不允许一个或多个给定 URL。它必须与网站匹配URL 或 Canvas URL,或者域必须是应用程序域之一的子域。"
  • .htaccess Apache 文件中的“RewriteEngine On”如何影响这个问题?
  • 来自 Apache 的奇怪东西!

标签: php jquery ajax post


【解决方案1】:

您的代码没有任何严重问题,但我认为最好使用data: {country: country_code} 而不是data: {'country': country_code}。也许这可以解决你的问题。

【讨论】:

  • 请检查您的浏览器控制台并告知结果。
  • 你是指Firebug吗?
【解决方案2】:

我发现了我的问题。 是 Apache .htaccess file 将 URL 更改为其他内容。 如果没有其他方法,请尝试检查您的 .htaccess 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多