使用AJAX在单独的文件中调用php函数 [英] Calling a php function in a separate file using AJAX

查看:96
本文介绍了使用AJAX在单独的文件中调用php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写货币转换器,需要使用jQuery和AJAX发送往返货币,以及要转换为PHP文件的值,这将返回转换后的值.

I've been writing a currency converter and need to use jQuery and AJAX to send the to and from currencies, and the value to convert to a PHP files, which will return the converted value.

我的解决方案基于:如何通过JavaScript调用PHP函数?但是它似乎不起作用.

My solution is based off of: How can I call PHP functions by JavaScript? however it doesn't seem to work.

jQuery代码:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

<script>
function exchange(from, to, amount){
    alert("got to here"); //this alert shows
    jQuery.ajax({
        type: "POST",
        url: 'exchange_caller.php',
        dataType: 'json',
        data: {functionname: 'exchange_rate_convert', arguments:["USD", "EUR", 1]},
        //dummy pass values

        success: function (obj, textstatus){
            if( !('error' in obj)){
                answer = obj.result;
                alert(answer);      //neither of these alerts show
                alert('anything');
            }
            else{
                console.log(obj.error);
                alert("got an error"); //this alert doesn't show
            }
        }
    });
    alert("passed the block"); //this alert shows
    return false;               //return false so the page doesn't refresh
}</script>

php文件"exchange_caller.php"(设置为虚拟文件)中的代码.

The code in the php file 'exchange_caller.php' (set up as a dummy).

<?php header('Content-Type: application/json');

$aResult = array();

if(!isset($_POST['functionname'])){
    $aResult['error'] = 'No function name!';
}
if(!isset($_POST['arguments'])){
    $aResult['error'] = 'No function arguments';
}
if(!isset($aResult['error'])){
    switch($_POST['functionname']){
        case 'exchange_rate_convert':
            if(!is_array($_POST['arguments']) || (count($_POST['arguments']) < 3)){
                $aResult['error'] = 'Error in arguments!';
            }
            else{
                $aResult['result'] = exchange_rate_convert($_POST['arguments'][0], $_POST['arguments'][1], $_POST['arguments'][2]);
            }
            break;

        default:
            $aResult['error'] = 'Not found function '.$_POST['functionname'].'!';
            break;
    }
}

echo json_encode($aResult);

function exchange_rate_convert($from, $to, $amount){    //function to run
    //dummy code, just return 2 for now
    $value = 2;
    return $value;
}?>

运行此命令时,我会收到到达此处"并通过了阻止"错误消息,但是当我应该返回结果时,都没有这两个消息.

When this runs I receive the 'got to here' and 'passed the block' error messages, but neither of the messages when I should be getting a result back.

任何帮助将不胜感激.

谢谢.

推荐答案

我使用发布的代码设置了文件,并且它们按预期运行.

I setup files with the posted code and they functioned as intended.

似乎还有另一个与所提供的代码无关的问题.

It looks like there is another issue unrelated to the code provided.

我建议确保您的托管环境没有问题.请查阅您的服务器日志,以查看是否有其他问题阻止您的请求得到满足,并在另一个浏览器中对其进行测试,以确保没有插件或扩展干扰该请求.

I suggest ensuring that there is not an issue with your hosting environment. Consult your server logs to see if another issue is preventing your request from being fulfilled and also test it in another browser to ensure that no addons or extensions are interfering with it.

这篇关于使用AJAX在单独的文件中调用php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆