Php 我使用JSON获得的警报结果
好的,我有以下几点。当用户将某些内容放入文本字段,然后失去焦点时,就会发出ajax请求Php 我使用JSON获得的警报结果,php,ajax,json,api,Php,Ajax,Json,Api,好的,我有以下几点。当用户将某些内容放入文本字段,然后失去焦点时,就会发出ajax请求 $(document).ready(function() { //On Focus lose get content of the first input field $('#fromCountry').blur(function() { var input = $('#fromCountry').val(); if(input == "") return false;
$(document).ready(function() {
//On Focus lose get content of the first input field
$('#fromCountry').blur(function()
{
var input = $('#fromCountry').val();
if(input == "")
return false;
else
{
$.ajax({
type: 'GET',
url: 'webservice.php',
data: {country: input, action: 'firstTime'},
success: function(response, textStatus, XMLHttpRequest) {
alert("TEST");
}
}
);
}
});
});
然后,它将请求发送到Web服务,并在该字段中键入国家,Web服务使用数据库类中的方法获取该国家(该国的国会大厦)的经度和纬度,并将其作为JSON返回
这是webservice.php
<?php
header("Content-type: application/json");
require('Database.php');
$db = new Database();
$action = $_GET['action'];
if($action == 'firstTime')
{
$country = $_GET['country'];
$result = $db->getLocation($country);
echo json_encode($result);
}
?>
这基本上是正确的,但是我的ajax函数没有像它应该的那样提醒“TEST”,因为它在success函数中。我忘记迈出一步了吗
-----编辑:
我现在补充说
,
error: function(){
alert("ERROR");
}
它总是进入误差函数,但为什么呢?我的意思是,它根据firebug得到响应,那么错误在哪里呢?更改为
function(response, textStatus, xhr) {
XMLHttpRequest是一个类名,您不能这样命名变量您知道HTTP代码响应是什么吗?您好,我更改了它,但它仍然不能提醒测试。它不知何故没有进入成功功能。我现在添加了
,error:function(){alert(“error”);}
,它进入了error函数,但是为什么?您安装了firebug吗?检查控制台中发生了什么。可能会出现404或403之类的http错误,正如我所说,firebug的XHR告诉我上面发布的代码。
,
error: function(){
alert("ERROR");
}
function(response, textStatus, xhr) {