4

我想用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;

?>

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

我明白了: 在此处输入图像描述

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

4

5 回答 5

3

您没有阻止表单提交,您正在使用表单和输入按钮提交类型。因此,当您单击该按钮时,您的表单正在提交。所以,首先你在你的 ajax 代码中停止你的表单提交。

第二件事是您在 ajax 代码中使用方法 get 并尝试通过 php 代码中的“POST”获取值。因此,请使用 $_GET 或更改 ajax 代码类型:'POST'

第三件事是您的网址无效,您应该使用 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);
    }
}

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

于 2013-05-22T12:57:01.900 回答
2

I am not sure but in your js code I see

 type: 'GET',

but in your php code use POST method to load value

if(isset($_POST['appid']) && !empty($_POST['appid'])) {
    $appid = $_POST['appid'];
}
于 2013-05-22T11:45:05.307 回答
0

您正在发出GETajax 请求,并且您依赖$_POST脚本中的信息,只需使用$_GET.

您会收到 undefined var 的通知$appid,单击 devtools 中的红色行以查看您收到的响应以及相关的错误代码。

于 2013-05-22T11:49:33.277 回答
0

尝试给予

网址:'loadjson.php'

在你的 js 文件中

于 2013-05-22T11:39:52.253 回答
0
$.ajax({
        type: 'GET',
        url: '../loadjson.php',
        datatype: 'json' ,
        data: {
            'appid': appid
        },
...
于 2013-05-22T12:17:02.033 回答