如何在ajax中执行一个名为php script的链接

时间:2014-08-02 04:24:57

标签: javascript php ajax curl

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

我尝试使用下面的curl pgm(全新卷曲)

  $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页面链接;

提前多多感谢!

1 个答案:

答案 0 :(得分:1)

我不知道你要做什么,但是让我告诉你:cURL是一个可以打开网站并返回输出的工具。它无法执行任何其他操作,例如执行javascript。也禁止Javascript使用与他们当前所在网站不同的网址来调用网址。因此,除非您的网站是api.smsgatewayhub.com,否则您无法向api.smsgatewayhub.com发出AJAX请求。我不知道你对'执行链接'的确切含义。

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

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