[英]Retrieve Data From PHP using JQuery
I'm not sure what I've done wrong with this but I have a php function that I need to retrieve some data from. 我不确定我做错了什么,但我有一个PHP函数需要从中检索一些数据。
I am using JQuery 1.11 to execute an ajax function 我正在使用JQuery 1.11执行ajax函数
$(document).ready(function() {
$('#butt_example').on('click', function() {
$.ajax({
url : 'scripts/functions.php',
type : 'GET',
dataType : 'json',
data : {
'action': 'test'
},
success: function(result) {
var la_result = JSON.parse(result);
console.log(la_result);
},
error: function(log) {
console.log(log.message);
}
});
});
});
Then I replaced my php function with a simple one just for testing 然后我用一个简单的php函数替换了它,仅用于测试
<?php
if(isset($_GET["action"])) {
$arr = array();
$arr[0] = "test";
$arr[1] = "passed";
header('Content-Type: application/json; charset=utf-8');
echo json_encode($arr);
}
?>
However when I click the button to execute the code I see that the console log result is undefined. 但是,当我单击按钮执行代码时,我看到控制台日志结果未定义。
Why is this? 为什么是这样?
UPDATE 更新
After Mike's comment bellow I realised that it's actually executing the console log in the error condition. 在Mike发表评论之后,我意识到它实际上是在错误情况下执行控制台日志。
So it's failing somewhere and I'm not sure why 所以它在某处失败了,我不确定为什么
Are u sure the AJAX call is able to reach the PHP function? 您确定AJAX调用能够到达PHP函数吗?
Try this In a .php file do this first 尝试此操作。在.php文件中首先执行此操作
This will produce the base_url() similar to codeigniter or any other relative framework. 这将产生类似于codeigniter或任何其他相对框架的base_url()。
base_url in codeigniter codeigniter中的base_url
function url(){
return sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['SERVER_NAME'],
$_SERVER['REQUEST_URI']
);
}
<script>
var url="<?php echo url(); ?>";
</script>
Then in your JS file supply the URL 然后在您的JS文件中提供URL
$(document).ready(function() {
$('#butt_example').on('click', function() {
$.ajax({
url : url+'scripts/functions.php',
type : 'GET',
dataType : 'json',
data : {
'action': 'test'
},
success: function(result) {
var la_result = JSON.parse(result);
console.log(la_result);
},
error: function(log) {
console.log(log.message);
}
});
});
});
As pointed out url extraction modified for both http and https. 如前所述,为http和https修改的网址提取。
As you can see from the manual : 正如您从手册中看到的 :
error
错误
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )类型:函数(jqXHR jqXHR,字符串textStatus,字符串errorThrown)
The first argument is a jqXHR
object. 第一个参数是
jqXHR
对象。
For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods:
为了与XMLHttpRequest向后兼容,一个jqXHR对象将公开以下属性和方法:
- readyState
readyState
- status
状态
- statusText responseXML and/or responseText when the underlying request responded with xml and/or text, respectively setRequestHeader(name, value) which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old one
当基础请求分别以xml和/或文本作为响应时,statusText responseXML和/或responseText分别为setRequestHeader(name,value),该请求通过使用新值替换旧值而不是将新值连接到旧值而偏离标准
- getAllResponseHeaders()
getAllResponseHeaders()
- getResponseHeader()
getResponseHeader()
- statusCode()
statusCode()
- abort()
abort()
So as you can see, there is no jqXHR.message
property. 如您所见,没有
jqXHR.message
属性。 So when you do: 因此,当您这样做时:
error: function(log) {
console.log(log.message);
}
of course, you're going to get an undefined notice because that property doesn't exist. 当然,您将收到未定义的通知,因为该属性不存在。
From this answer , to get the error message you need to do: 从此答案中 ,要获取错误消息,您需要执行以下操作:
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
Works for me with jquery 1.12.3 适用于我与jQuery 1.12.3
success: function(result) {
alert(result);
},
//shows "test,passed" //显示“测试通过”
Next line of JSON returns in the console log: JSON.parse: unexpected keyword at line 1 column 1 of the JSON data 控制台日志中返回下一行JSON:JSON.parse:JSON数据的第1行第1行出现意外的关键字
It seems the error was related to the json_encode function. 似乎错误与json_encode函数有关。
CentOS 5 php installation does not include the json.so module so it had to be manually installed which then fixed the error. CentOS 5 php安装不包括json.so模块,因此必须手动安装它,然后才能修复错误。
Thanks to everyone for their help though it's greatly appreciated 感谢大家的帮助,对此深表感谢
Try this: 尝试这个:
$(document).ready(function() {
$('#butt_example').on('click', function() {
$.ajax({
url : 'scripts/functions.php',
type : 'GET',
dataType : 'json',
data : 'action=test',
success: function(result) {
var la_result = JSON.parse(result);
console.log(la_result);
},
error: function(log) {
console.log(log.message);
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.