【问题标题】:Performing a PHP redirect for an API during a jquery ajax get returning 302在 jquery ajax 期间为 API 执行 PHP 重定向返回 302
【发布时间】:2013-07-26 14:21:12
【问题描述】:

我正在通过jQuery ajax 调用一个 php 页面,该页面连接到 API,获取请求令牌,执行标头重定向回同一页面,而不是在返回时返回一个 json 编码字符串。但是,它返回 302 错误。

我的问题是,我该如何实现它才能正常工作?

【问题讨论】:

标签: php jquery ajax redirect


【解决方案1】:

这里我提供了一个包含 2 个文件的基本示例。

index.html

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
    var param = 'name=YourName';
    var request = $.ajax({
        url: "phpcallback.php",
        type: "POST",
        data: param,
        dataType: "json"
    });     
    request.done(function(response) {       
        $("#Mydiv").html(response); 
    });
</script>
</head>
<body>
<div id="Mydiv">
Click to get json response from server
</div>
</body>
</html>

phpcallback.php

<?php
  $Name = $_POST['name'];
  $Arr = array();
  $cn = mysql_connect("localhost","root","");
  mysql_select_db("mydb", $cn);
  $Query = mysql_query("select * from mytable where name = '$Name'",$cn);
  $Row = mysql_fetch_array($Query);     
  $Arr = array('Null');     
  $Arr = array($Row['Name'],$Row['Email'],$Row['Address'],$Row['Phone']);   
  echo json_encode($Arr);   
?>

只要您有相应的数据库设置,此代码应该可以工作

问候
伊曼纽尔·保罗

【讨论】:

    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 2021-03-03
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多