php+jquery+ajax+json实现跨域调用 php pdo操作oracle数据库

<?php
/*
 * Created on 2012-7-4
 *
 * write  by  harry
 *
 */

 echo
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<title></title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
* { margin:0; padding:0;}
body { font-size:12px;}
.comment { margin-top:10px; padding:10px; border:1px solid #ccc;background:#DDD;}
.comment h6 { font-weight:700; font-size:14px;}
.para { margin-top:5px; text-indent:2em;background:#DDD;}
</style>
 <!--   引入jQuery -->
<script src="../scripts/jquery-1.3.1.js" type="text/javascript"></script>

</head>
<body>
 <br/>
 <p>
  <input type="button" id="send" value="加载"/>
 </p>
<script> $(function() { $.getJSON(\'http://127.0.0.1/test.php?callback=?\',{no:"123456789",name:"ecshop"},function(json){ alert(json.key); }); }); </script>
<div  class="comment">已有评论:</div>
 <div id="resText" >

 </div>

</body>
</html>';
?>
____________________________________

<?php
$dbh = new PDO('oci:dbname=ecshop, 'root,'root');
$no=$_GET['no'];
$name=$_GET['name'];
if($no&&$name)
{

 $results = array("key" => "$name");
 
 echo $_GET['callback'] . '(' . json_encode($results) . ')';

 

 $stmt = $dbh->prepare("insert  into EMP(emp,name )  VALUES (:no, :name)");
 $stmt->bindParam(':no',     $no,      PDO::PARAM_STR, 30);

 $stmt->bindParam(':name',          $name,          PDO::PARAM_STR, 30);
  
 
  

 

 
   $stmt->execute();

   
}

 


/*
$callback = isset($_GET["callback"]) ? $_GET["callback"]:"callback";
$getstr = isset($_GET["ajaxstr1"]) ? $_GET["ajaxstr1"] : "";
$data = array('type'=>1);
echo $_GET['callback'].'('.json_encode($data).')';
exit;
*/

?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Java和JavaScript中的AJAX和CORS来实现跨域请求,可以按照以下步骤进行设置: 在Java后端: 1. 在处理跨域请求的Java代码中,添加以下响应头,以启用CORS: ```java response.setHeader("Access-Control-Allow-Origin", "http://example.com"); // 允许特定的源访问 response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); // 允许的HTTP方法 response.setHeader("Access-Control-Allow-Headers", "Content-Type"); // 允许的请求头 response.setHeader("Access-Control-Allow-Credentials", "true"); // 是否允许发送Cookie response.setHeader("Access-Control-Max-Age", "3600"); // 预检请求的有效期 ``` 2. 在处理OPTIONS请求的方法中添加以下响应头,以处理预检请求(preflight request): ```java response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); response.setHeader("Access-Control-Allow-Headers", "Content-Type"); response.setHeader("Access-Control-Max-Age", "3600"); response.setStatus(HttpServletResponse.SC_OK); ``` 在JavaScript前端: 3. 使用XMLHttpRequest或fetch API发送跨域请求,并在请求头中添加Origin字段: ```javascript var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://api.example.com/data', true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.setRequestHeader('Origin', 'http://example.com'); xhr.withCredentials = true; // 允许发送Cookie xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { var response = JSON.parse(xhr.responseText); // 处理响应 } }; xhr.send(); ``` 这样,Java后端设置了CORS支持,允许特定源访问资源,并在JavaScript前端发送跨域请求时设置了请求头,指示该请求是一个跨域请求。同时,设置了withCredentials属性为true,以允许发送Cookie。 请注意,实际应用中需要根据需求和安全考虑进行适当的配置和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值