ajax可以读取php文件吗,从php文件ajax获取数据

我想在javascript中获取我的PHP文件中加载的数据.

这就是我做的:

$("#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");

}

}

});

});

当我单击一个按钮时,我得到一个文本框的值并调用ajax函数.

我的javascript文件位于root / js / file.js中,我的php文件位于root / loadjson.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;

?&GT

问题是我总是得到一个警告框“出错了”,但我找不到解决方案.有人看到我的错吗?

我明白了:

解决方法:

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 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);

}

}

希望,你明白你错在哪里:)

标签:json,php,javascript,ajax,curl

来源: https://codeday.me/bug/20190620/1243918.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值