php中ajax请求
来源:互联网 发布:网络信息安全实施方案 编辑:程序博客网 时间:2024/05/17 04:06
php中ajax请求的时候,网上一般都是如下这个样子:
<html><head><script>function showHint(str){ if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码 xmlhttp=new XMLHttpRequest(); } else { //IE6, IE5 浏览器执行的代码 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","gethint.php?q="+str,true); xmlhttp.send();}</script></head><body><p><b>在输入框中输入一个姓名:</b></p><form> 姓名: <input type="text" onkeyup="showHint(this.value)"></form><p>返回值: <span id="txtHint"></span></p></body></html>
服务端:
<?php// 将姓名填充到数组中$a[]="Anna";$a[]="Brittany";$a[]="Cinderella";$a[]="Diana";$a[]="Eva";$a[]="Fiona";$a[]="Gunda";$a[]="Hege";$a[]="Inga";$a[]="Johanna";$a[]="Kitty";$a[]="Linda";$a[]="Nina";$a[]="Ophelia";$a[]="Petunia";$a[]="Amanda";$a[]="Raquel";$a[]="Cindy";$a[]="Doris";$a[]="Eve";$a[]="Evita";$a[]="Sunniva";$a[]="Tove";$a[]="Unni";$a[]="Violet";$a[]="Liza";$a[]="Elizabeth";$a[]="Ellen";$a[]="Wenche";$a[]="Vicky";//从请求URL地址中获取 q 参数$q=$_GET["q"];//查找是否由匹配值, 如果 q>0if (strlen($q) > 0){ $hint=""; for($i=0; $i<count($a); $i++) { if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { if ($hint=="") { $hint=$a[$i]; } else { $hint=$hint." , ".$a[$i]; } } }}// 如果没有匹配值设置输出为 "no suggestion" if ($hint == ""){ $response="no suggestion";}else{ $response=$hint;}//输出返回值echo $response;?>网上都是这样,原生的ajax请求。但是php有自己封装的ajax请求。操作也比较方便。现在我附录一个比较全面的demo:
<?php $billingUrl = 'http://localhost/smart/billing.html'; $amount = $_REQUEST['amount']; if($amount != 0){ $result = ""; $email = $_REQUEST['email']; $company = $_REQUEST['company']; $date = $_REQUEST['date']; $billingAddress = $_REQUEST['billingAddress_name']. " ".$_REQUEST['billingAddress_address_line1']. " ".$_REQUEST['billingAddress_postal_code']. " ".$_REQUEST['billingAddress_city']. " ".$_REQUEST['billingAddress_country']. " ".$_REQUEST['billingAddress_province']; $shippingAddress = $_REQUEST['shippingAddress_name']." ". $_REQUEST['shippingAddress_address_line1']." ". $_REQUEST['shippingAddress_postal_code']." ". $_REQUEST['shippingAddress_city']." ". $_REQUEST['shippingAddress_country']." ". $_REQUEST['shippingAddress_province']; $currency = $_REQUEST['currency']; $tax = $_REQUEST['tax']; $itemDescription = $_REQUEST['itemDescription']; //$comment = $_REQUEST['comment']; $code = $_REQUEST['cardInfo_code']; $cardInfo_name = $_REQUEST['cardInfo_name']; $billingStr = '{"email":"'. $email . '","company":"'. $company . '","date":"'. $date .'","code":"'. $code . '","transId":"0","billingAddress":"'. $billingAddress . '","shippingAddress":"'. $shippingAddress . '","amount":"'. $amount . '","currency":"'. $currency . '","tax":"'. $tax . '","itemDescription":"'. $itemDescription . '","comment":"","paymentStatus":"false"}'; $insertBillingReturn = json_decode(insertBillingTable('http://localhost:3000/billing',$billingStr)); if($insertBillingReturn -> success == 1){ $id = $insertBillingReturn -> id; //$paymentStr = '{"payment_method":"token","order_number":"MyOrderId000011223344","amount":"' . $amount . // '","token":{"code":"' . $code . // '","name":"' . $cardInfo_name . '","complete":true}}'; //$beanstreamReturn = json_decode(beanstreamPayment('https://www.beanstream.com/api/v1/payments', $paymentStr)); //if($beanstreamReturn -> code != null){ // echo "<script>alert('".$beanstreamReturn -> message."');location.href='".$billingUrl."';</script>"; // exit; //} //else{ // $transId = $beanstreamReturn -> id; // $str = '{"id":"'. $id .'","transId":"'. $transId .'","paymentStatus":"true"}'; // $updateBillingReturn = json_decode(updateBillingTable('http://localhost:3000/billing',$str)); // if($updateBillingReturn -> success == 1){ echo "<script>alert('success');location.href='".$billingUrl."';</script>"; exit; // } // else{ // echo "<script>alert('".$updateBillingReturn -> error."');location.href='".$billingUrl."';</script>"; // exit; // } //} } else{ echo "<script>alert('".$insertBillingReturn -> error."');location.href='".$billingUrl."';</script>"; exit; } } else{ echo "<script>alert('The amount is 0.');location.href='".$billingUrl."';</script>"; exit; } function beanstreamPayment($url, $post_data) { $options = array( 'http' => array( 'method' => 'POST', 'header' => array( 'Content-type:application/json', 'Authorization:Passcode MzAwMjAwNTc4OjRCYUQ4MkQ5MTk3YjRjYzRiNzBhMjIxOTExZUU5Zjcw' ), 'content' => $post_data, 'timeout' => 15 * 60 ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } function insertBillingTable($url, $post_data) { $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/json', 'content' => $post_data, 'timeout' => 15 * 60 ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } function updateBillingTable($url, $post_data) { $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/json', 'content' => $post_data, 'timeout' => 15 * 60 ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; }?>下面是我自己更改出来适用自己的ajax,完整代码如上。
$alipayStr = '{"out_trade_no":"'. $out_trade_no .'","trade_no":"'. $trade_no .'","trade_status":"'. $trade_status .'","subject":"'. $subject .'","total_amount":"'. $total_amount .'","body":"'. $body .'"}';$insertBillingReturn = json_decode(insertAlipayTable('http://localhost:3389/alipayZhifubao',$alipayStr));if($insertBillingReturn -> success == 1) { }else{}function insertAlipayTable($url, $post_data) {$options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/json', 'content' => $post_data, 'timeout' => 15 * 60 ) ); $context = stream_context_create($options);$result = file_get_contents($url, false, $context); return $result; }
阅读全文
0 0
- php中ajax请求
- 在PHP中如何判断AJAX请求
- php中检查是否ajax请求
- php 中ajax get与post请求
- PHP 和 AJAX 请求
- PHP检测 ajax 请求
- php ajax 请求原理
- PHP 和 AJAX 请求
- PHP----Ajax异步请求
- PHP----Ajax异步请求
- PHP 和 AJAX 请求
- php判断ajax请求
- PHP实现AJAX请求
- AJAX PHP请求实例
- PHP开发框架kohana中处理ajax请求的例子
- PHP开发框架kohana中处理ajax请求
- ajax请求php中生成的html代码
- 在php中判断一个网页请求是ajax请求还是普通请求
- 11月营销活动必备:怎样策划网站活动?
- spring源码分析:resource资源定位一
- JDK 1.8 官网下载地址(linux / windows)
- 11月7日云栖精选夜读:阿里巴巴风鸣:做技术Leader要有危机意识
- 你那么努力地学习,还不如去打几局王者荣耀
- php中ajax请求
- 社群系统ThinkSNS-plus移动端APP 2017年11月第一周研发播报
- 实战案例:AI产品经理如何介绍“计算机视觉”?
- mysql通过ip地址无法访问数据库
- Thinking in java 读书笔记(一、静态导入,多进制转换API)
- Qt实例-UCF数据标注2
- Java 线程问题
- 产品总监进阶|突破晋升瓶颈,实现3-5年产品经理的完美进阶
- 前端优化知多少