[英]Status 200 OK, same domain, valid JSON data and no response (Ajax)
Here's my ajax call: 这是我的ajax电话:
$.ajax({
url : hostGlobal + "site/modulos/prefeitura/acoes-jquery.php",
type: "POST",
dataType : "JSON",
data: {
acao: "filtrarCidades",
estado_id: $(".estados:chosen").val()
},
success: function(json) {
console.log("worked");
$(".cidades").html('');
var options = "<option value=\"\"></option>";
$.each(json, function(key, value) {
options += '<option value="' + key + '">' + value + '</option>';
});
$(".cidades").html(options);
if (!filterThroughCEP) {
$(".cidades").trigger("chosen:updated");
}
},
error: function(e) {
console.log(e.responseText);
}
});
Here's the php action: 这是php动作:
if ($acao == 'filtrarCidades') {
$estado_id = $_POST['estado_id'];
$cidade->where = "estado_id = '".$_POST['estado_id']."'";
$cidade->LoadFromDB();
for ($c=0; $c<count($cidade->itens); $c++) {
$cidades[$cidade->itens[$c]->id] = $cidade->itens[$c]->nome;
}
echo json_encode($cidades);
die();
}
json_encode($cidades)
is valid json data (UTF8), here's one example using debug: json_encode($cidades)
是有效的json数据(UTF8),这是一个使用debug的示例:
{"1778":"Bras\u00edlia"}
This {"1778":"Bras\ília"}
goes as e.responseText (Error), even with Status OK, and the URL is on the same domain (No need for JSONP). 即使状态为OK,此{"1778":"Bras\ília"}
仍为e.responseText(错误),并且URL在同一域中(不需要JSONP)。 I have no idea why I can't reach success
. 我不知道为什么我无法success
。
EDIT: I've set the contentType: 编辑:我已经设置了contentType:
contentType: "application/json",
And the call still can't "reach" success. 而且呼叫仍然无法“达到”成功。 Here's the third error argument: 这是第三个错误参数:
SyntaxError: Unexpected token
at parse (native)
at ajaxConvert (http://localhost/sisconbr-sistema-novo/site/visual/js/jquery.js:7608:19)
at done (http://localhost/sisconbr-sistema-novo/site/visual/js/jquery.js:7363:15)
at XMLHttpRequest.<anonymous> (http://localhost/sisconbr-sistema-novo/site/visual/js/jquery.js:7835:9)
It is indeed related to unicode characters inside the strings that come from the database. 实际上,它与来自数据库的字符串中的unicode字符有关。
EDIT2 : I wrote the whole thing again, and now it's clearer: EDIT2 :我再次写了整个内容,现在更清楚了:
function getCitiesByState() {
$.ajax({
url : hostGlobal + "site/estrutura/ajax.php",
type: "POST",
dataType : "text",
data: {
action: "getCitiesByState",
state_id: $(".estados option:selected").val()
},
success: function(json, textStatus, jqXHR) {
console.log($.parseJSON(json));
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
}
PHP: PHP:
if ($_POST["action"] == "getCitiesByState") {
$cities = getResults("SELECT * FROM tbl_cidades WHERE estado_id = ".$_POST["state_id"]);
echo json_encode($cities, JSON_UNESCAPED_UNICODE);
die();
}
Output: 输出:
[{"id":"1778","estado_id":"7","nome":"Brasília","cep":"","loc_no_abrev":"Brasília"}]
Error: 错误:
Uncaught SyntaxError: Unexpected token
I think that the problem is the object property {"1778":"Bras\ília"} means an object with an invalid property name, thus json decoding fails; 我认为问题在于对象属性{“ 1778”:“ Bras \\ u00edlia”}意味着对象名称无效,因此json解码失败; to prove if this is right try either 证明这是正确的尝试
it 1778 is an ID, a proper structure should be {id:"1778",property:"Bras\ília"} and work flawless give it a try and let us know 如果1778是ID,则正确的结构应为{id:“ 1778”,property:“ Bras \\ u00edlia”},并且可以完美地工作,请尝试一下并告知我们
EDIT: as jcaron kindly suggested, i have to fix, this answer: the "1778" is indeed a valid property name, but invalid identifier if dot notation is used. 编辑:正如jcaron所建议的那样,我必须解决此问题:“ 1778”确实是有效的属性名称,但是如果使用点表示法,则标识符无效。 Since I don't know how jQuery manage this i would suggest to test as above, and see if one of the tests gives results. 由于我不知道jQuery是如何管理的,因此我建议按上述方法进行测试,并查看其中一项测试是否给出结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.