【问题标题】:Sending flow to switch's default case将流发送到交换机的默认情况
【发布时间】:2012-06-21 08:16:56
【问题描述】:

大家好,

我的 js 文件中有:

function sendDataToPhp()
{
  theUrl = 'myfile.php';
    params = '';
    params += 'function=sendData';

    params += '&myfield='+someVariableForMyField;
    $.ajax ({
        url: theUrl,
        data: params,
        type: "POST",
        async:false,
        success: function (data, textStatus)
        {    
        }
    }); 

}

然后在myfile.php中

if (isset($_REQUEST['function']))
    {
        $function = $_REQUEST['function'];

    }  


    switch ($function) {
        case 'sendData':
        //code
        break;
        default:
       //code
}

有了这个,我将代码流发送到交换机的 sendData 案例。

现在在success里面,在ajax()中我需要将代码流发送到switch的default case。我怎样才能做到这一点?

谢谢一百万!

【问题讨论】:

  • 只是不要在参数中发送function=sendData,它将转到default,如果您可以将其作为sendDataToPhp(function_name) 中的参数会更好,这样您就可以重复使用它有或没有它
  • 有什么办法可以做我要求的事情(在成功的内部,在 ajax() 流程中转到默认情况)?否则我将不得不更改很多代码
  • 如果你将它作为sendDataToPhp('function=sendData')sendDataToPhp('function=dontexist') 的参数

标签: php jquery ajax switch-statement


【解决方案1】:

Switch 的工作方式是,如果没有匹配的 CASE,则转到 DEFAULT 案例...所以也许您可以在 ajax 调用中提供function=IdontExistFunction ...或者如果您在 DEFAULT 中有一段代码想运行的话最好把它分开,让它成为一个函数,这样你就可以根据需要从任何地方调用。

顺便说一句 - 也有 GOTO 语句可用 - 不确定您是否可以在您的情况下使用它 http://php.net/manual/en/control-structures.goto.php

【讨论】:

  • 关于 IdontExistFunction 是个好主意,但我想没有办法发送参数 += 'function=IdontExistFunction'; ajax() 里面的成功。我说的对吗?
  • goto应该是jquery语句,我是通过js文件控制流
  • 我从来没有尝试在 ajax succes 函数中进行另一个 ajax 调用 - 也许你可以尝试调用你的 JS 函数来使用 IDontExist 参数进行另一个 ajax 调用 - 但我认为你的方法/逻辑无论你试图实现的地方是错误的
  • IdontExistFunction 方法在成功的 ajax() 之外起作用。感谢一百万的线索!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多