本文介绍了字符串数据未通过$ .ajax调用发送到php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我传递带有函数参数的数字以使用ajax发送时,它的工作正常,但是当我传递下面给出的字符串时,它在php页面中没有收到.

When i pass number with function argument for sending using ajax its working properly but when i pass string such as given below its not receiving in php page.

function loadState(country) {
  $.ajax({
    url: 'cardstate.php',
    type: 'get',
    data: { 'country': country },
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    success: function(data) {
      alert(data);
    }
  });
}

loadState("Pakistan")
// cardstate.php:
$country = $_GET['country'];
echo $country; // not receving string while when i put number its receiving

推荐答案

在cardstate.php中,您应该使用json_encode

In your cardstate.php you should use json_encode

// cardstate.php:
$country = $_GET['country'];
echo json_encode($country); // not receving string while when i put number its receiving

这篇关于字符串数据未通过$ .ajax调用发送到php页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:39