【问题标题】:how to execute a link in ajax called php script如何在名为 php 脚本的 ajax 中执行链接
【发布时间】:2014-08-02 04:24:56
【问题描述】:

我正在尝试执行链接,以便该页面不会重定向,而只是执行链接..

我尝试了以下 curl pgm(对 curl 来说是全新的)

  $ch = curl_init(); // Initializing
curl_setopt($ch, CURLOPT_URL,"http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd=429944&to=9176411081&sid=STMTSC&msg=Dear Sam,%20choir%20practice%20will%20be%20held%20in%20our%20Church%20on%20July%2031%20at%208:00%20pm.%20Thanks,%20St.%20Thomas%20MTC!&fl=0&gwid=2"); // Set URI
 curl_setopt($ch, CURLOPT_HEADER, 0); //Set Header
 curl_setopt($ch, CURLOPT_TIMEOUT, 300); // Time-out in seconds
$result = curl_exec($ch); // Executing
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
$result = ""; // Executing
}
curl_close($ch); // Closing the channel
return $result;

但这并没有按预期工作...我尝试在 php 中使用 javascript 代码回显,但是由于该 php 文件是通过 ajax 调用的,因此该脚本没有被执行.. :(

调用 pgm

  <div class="button_div">
                     <input type="button" onclick="send_sms()" value="Send SMS" class="submit">  
            </div>

和 javascript 函数

    function send_sms()
        {
            var sms_msg=(document.getElementById('preview_box').value).trim();
    /*        var prayer_group_nm=(document.getElementById('prayer_group').value).trim();
            if ((recepient==='prayer_grp')&&(prayer_group_nm==='select'))
            {
                alert('Select the appropriate Prayer group');
                return;
            }
            */

            if (sms_msg==='')
            {
                alert ('Preview MSG prior to sending!');
            }
            else
            {

                myurl = '../sp/send_sms_sp.php';
                parms="?recepient="+recepient+"&sms_msg="+sms_msg;
                modurl = myurl+parms;  
                if (recepient==='prayer_grp')
                {
                    modurl=modurl+"&prayer_group_nm="+prayer_group_nm;
                }

                response='sms_templates_response';  //setting div where response is being displayed
                swtch='send_sms_state';

                ajax_call.call();

                //removing earlier checked state of radio buttons
                $("input:radio").attr("checked", false);
                document.getElementById('preview_box').value='';

                //hiding out unwanted divs
                $(".button_div").hide();
                $("#preview").hide();

            }

        }

还有 ajax_call() 函数...

        function ajax_call()            
        {
          var xmlhttp;    
          {
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState===4 && xmlhttp.status===200)
                {
                    if (swtch!=='preview')
                    {
                        document.getElementById(response).innerHTML=(xmlhttp.responseText);    
                    }
                    else
                    {
                        raw_template=xmlhttp.responseText; 
                                                    //do not move the below code to the calling function - as it has a bug.. will not work as expected, if done
                        modified_msg='';
                        get_modified_msg.call();
                        document.getElementById(response).value='Dear (name), '+modified_msg.trim()+'. Thanks, St. Thomas MTC!';
                    }
                   // return;
                }
                else 
                {
                    if (swtch!=='preview')
                    {
                        document.getElementById(response).innerHTML ='<img src="../animation/ajax-loader.gif">';
                    }

                }
              }
                xmlhttp.open("GET", modurl, true);
                xmlhttp.send();
            }  // return;
    };

和被调用的php

 <?php
//retreiving data from db and then below code
        $sender_id='STMTSC';
            $pwd='429999';
            $url='http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd='.$pwd.'&to='.$mob.'&sid='.$sender_id.'&msg='.$msg.'&fl=0&gwid=2';

            echo $url.'<br>';

            echo '<script src="../jquery/jquery-1.10.1.js">';
            echo '</script>';

            echo '        <script>';
           // echo '            $(document).ready(function(){';


            echo '            $.ajax({';
            echo '            type: "POST",';
                 //       url: "http://api.smsgatewayhub.com/smsapi/pushsms.aspx?user=stthomasmtc&pwd=429944&to=9176411081&sid=STMTSC&msg=Dear Sam,%20choir%20practice%20will%20be%20held%20in%20our%20Church%20on%20July%2031%20at%208:00%20pm.%20Thanks,%20St.%20Thomas%20MTC!&fl=0&gwid=2",
            echo '            url: "'.$url.'" ,';
            echo '            data: { message: "hello" }';
            echo '            })';
            echo '            .done(function( msg ) {';
            echo '            alert( "script executed successfully ");';
            echo '            this.preventDefault();'; // stops navigating from the page while <a> click or on submit click.
            echo '            }); ';
            //echo '        });';
            echo 'window.alert("ending script")';
            echo '         </script>';

//update of db as log

userid 和 pwd 在这种情况下无效; 因为它在 php 中的脚本是通过 ajax 调用的,所以事情不工作:( 能否请您提供一种方法来执行通过 ajax 调用的 php 页面中的链接;

提前非常感谢!

【问题讨论】:

  • 你怎么打电话给send_sms()
  • “执行链接”是什么意思?你的意思是当你点击链接时调用一个Javascript函数?使用href="javascript:send_sms()"
  • 您应该在所有 AJAX 参数周围加上 encodeURIComponent。否则,如果它们包含空格或其他特殊字符,您将遇到问题。
  • @Barmar 感谢您的回复.. 是的,我通过按钮单击调用 send_sms() - onclick 事件...当我尝试直接执行 php 脚本时,其中的 javascript 工作;但是当我尝试通过 ajax 调用它时,它不起作用..(我的整个工作都停滞不前,因为我找不到出路:()你能告诉我我需要做的更正吗......当我用谷歌搜索时,它说我应该使用 eval() 函数..但不知道如何使用它..
  • 将调用send_sms的代码添加到您的问题中。

标签: javascript php ajax curl


【解决方案1】:

我不知道您要做什么,但让我告诉您:cURL 是一个可以打开网站并返回输出的工具。它不能做任何其他事情,比如执行 javascript。也禁止 Javascript 调用与其当前所在网站不同的 url。因此,除非您的网站是 api.smsgatewayhub.com,否则您无法向 api.smsgatewayhub.com 发出 AJAX 请求。我不知道您所说的“执行链接”到底是什么意思。

查看 smsgatewayhub.com 提供的示例 PHP SMS API。

http://www.smsgatewayhub.com/sms-gateway-api/php-sms-script

【讨论】:

  • 我确实先检查了这个文档.. 但不确定要替换哪个代码以满足我们的需要...如果您能指导我,我将不胜感激(我与客户支持进行了核对团队,他们拒绝回应)非常感谢!
  • 好吧,我真的不知道脚本。首先,您可能应该稍微清理一下示例代码(例如缩进,制作更多空格)。也许那时您就可以阅读代码。 php 开发人员应该能够阅读陌生人的 php 代码,如果不能,因为空格太少或没有缩进,那么做必要的事情使其更具可读性。我也必须这样做。
猜你喜欢
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 2011-08-18
  • 1970-01-01
  • 2014-12-07
相关资源
最近更新 更多