php - 从php文件+ajax获取数据

标签 php javascript ajax json curl

我想用 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 文件:

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

?>

问题是我总是收到一个警告框,其中显示“出了点问题”,但我找不到解决方案。有人看出我的错了吗?

我明白了: enter image description here

jsfiddle:http://jsfiddle.net/wKe2U/

最佳答案

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

希望,您明白自己哪里错了:)

关于php - 从php文件+ajax获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16690375/

相关文章:

javascript - 如何使用 javascript 将 LatlngBounds 对象传递给 nodejs 服务器

php - 在 IF 语句中更新数据库

php - Laravel 中的数组推送

php - 内连接2个表,并在while中获取结果

PHP Include 不显示页眉和页脚

javascript - 在 React Native 中按下时更改按钮样式

javascript - 为什么 ng-repeat 中的函数会被调用多次?

ruby-on-rails - AngularJS 无限滚动调用 API 有效但不显示新记录

java - 为什么 @ResponseBody 在 Spring MVC 的 void 方法上显示没有在 Firefox 控制台中找到元素?

javascript - googlebot 需要的 HTML snapshot 是否需要设置样式