php 从php文件+ajax获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16690375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get data from php file + ajax
提问by nielsv
i want to get data that's loaded in my PHP file in javascript. This is what I do:
我想获取加载到我的 PHP 文件中的 javascript 数据。这就是我所做的:
$("#submit").click(function() {
// GET VALUE OF APPID
var appid = $("#appid").val()
// GET JSON FROM PHP SCRIPT
$.ajax({
type: 'GET',
url: '../loadjson.php',
data: {
'appid': appid
},
success: function (data) {
alert('success');
},
error: function(jqXHR,error, errorThrown) {
if(jqXHR.status&&jqXHR.status==400){
alert(jqXHR.responseText);
}else{
alert("Something went wrong");
}
}
});
});
When I click a button I get the value of a textbox and call ajax function. my javascript file is located in root/js/file.js and my php file in root/loadjson.php
当我点击一个按钮时,我会得到一个文本框的值并调用 ajax 函数。我的 javascript 文件位于 root/js/file.js 中,我的 php 文件位于 root/loadjson.php
My PHP file:
我的PHP文件:
<?php
if(isset($_POST['appid']) && !empty($_POST['appid'])) {
$appid = $_POST['appid'];
}
$json_url ='http://api.url.com/api/gateway/call/1.4/getApp?appid=' . $appid;
$ch = curl_init($json_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$str = curl_exec($ch);
curl_close($ch);
$data = json_decode($str);
$array = $data;
$object = $array->app[0];
echo $object;
?>
?>
The problem is I get always an alert box with "Something went wrong" but I can't find the solution. Does someone see my fault?
问题是我总是收到一个带有“出现问题”的警告框,但我找不到解决方案。有人看到我的错了吗?
I get this:
我明白了:
jsfiddle: http://jsfiddle.net/wKe2U/
jsfiddle:http: //jsfiddle.net/wKe2U/
采纳答案by Neeraj Singh
You are not preventing your form submission, you are using form and input button submit type. So, while you clicking on that button your form being submit. So, first you stop your form submission in your ajax code.
Second thing is that you are using method get in your ajax code and trying to get values by 'POST' in your php code. So, kindly use $_GET or change ajax code type: 'POST'
Third thing is that your url is invalid you should use url:'loadjson.php'
您不是在阻止表单提交,而是在使用表单和输入按钮提交类型。因此,当您单击该按钮时,您的表单正在提交。所以,首先你在你的 ajax 代码中停止你的表单提交。
第二件事是您在 ajax 代码中使用方法 get 并尝试通过 'POST' 在您的 php 代码中获取值。因此,请使用 $_GET 或更改 ajax 代码类型:'POST'
第三件事是您的网址无效,您应该使用 url:'loadjson.php'
here I am sharing code:
我在这里分享代码:
//Ajax code
$(function () {
$("#submit").click(function (e) {
// stop form submission first
e.preventDefault();
// GET VALUE OF APPID
var appid = $("#appid").val()
// GET JSON FROM PHP SCRIPT
$.ajax({
type : 'POST',
url : 'loadjson.php',
data: {'appid':appid},
success : function (d) {
alert(d);
},
error : errorHandler
});
});
});
function errorHandler(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
Hope, you understand where you were wrong :)
希望,你明白你错在哪里了:)
回答by franki3xe
I am not sure but in your js code I see
我不确定,但在你的 js 代码中我看到
type: 'GET',
but in your php code use POST method to load value
但在您的 php 代码中使用 POST 方法加载值
if(isset($_POST['appid']) && !empty($_POST['appid'])) {
$appid = $_POST['appid'];
}
回答by moonwave99
You are issuing a GET
ajax request, and you are relying on $_POST
information in your script, just use $_GET
.
您正在发出GET
ajax 请求,并且您依赖$_POST
脚本中的信息,只需使用$_GET
.
You are getting a notice for undefined var $appid
, click on the red row in your devtools to see the response you are getting, and associated error code.
您会收到 undefined var 的通知$appid
,单击 devtools 中的红色行以查看您收到的响应以及相关的错误代码。
回答by Amir
$.ajax({
type: 'GET',
url: '../loadjson.php',
datatype: 'json' ,
data: {
'appid': appid
},
...
回答by Malik Bilal
Try to give
尝试给予
url: 'loadjson.php'
网址:'loadjson.php'
in your js file
在你的 js 文件中