简体   繁体   中英

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

$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?

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} . Maybe this can solve your problem.

I found my problem. It was the Apache .htaccess file that was changing the URL to something else. If nothing else works, try checking your .htaccess file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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