简体   繁体   English

带有 POST 的 jQuery Ajax 发送字符串不起作用

[英]jQuery Ajax with POST to send string doesn't work

I have already seen all the q&a here, and I couldn't fix the problem.我已经看到这里的所有问答,我无法解决问题。 I'm trying to send a two character string.我正在尝试发送一个两个字符的字符串。

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");
  }
});

on GetData.php I just have在 GetData.php 上我只有

$cty = $_POST['country'];

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

Though, it doesn't work.虽然,它不起作用。 I can't get the string on GetData.php What am I doing wrong?我无法在 GetData.php 上获取字符串我做错了什么?

Your code does not have any serious problem, but I think It's better to use data: {country: country_code} instead of data: {'country': country_code} .您的代码没有任何严重问题,但我认为最好使用data: {country: country_code}而不是data: {'country': country_code} Maybe this can solve your problem.也许这可以解决您的问题。

I found my problem.我发现了我的问题。 It was the Apache .htaccess file that was changing the URL to something else.Apache .htaccess 文件将 URL 更改为其他内容。 If nothing else works, try checking your .htaccess file.如果没有其他效果,请尝试检查您的 .htaccess 文件。

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

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