【问题标题】:How to show jsonp response sent by php on a page如何在页面上显示php发送的jsonp响应
【发布时间】:2016-02-15 20:19:13
【问题描述】:

我有一个jsonp的脚本如下

<script>
$(document).ready(function(){

    $("#LoginForm").submit(function(){

    var data = $(this).serialize();
    //alert(data);
    $.ajax({
            type:"POST",
            dataType:"jsonp",
            url:"https://akshay.tk/api/login.php",
            data:data,

            success:function(data)
            {       

                /// WHAT TO WRITE HER TO GET PHP RESPONSE
                /// DIV WHERE DATA TO BE SHOWN, ID IS RESULT    
            }

            });
    return false;
    });

});
</script>

我的php代码是

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];              

// login check for user
$result = mysqli_query($con,"SELECT * FROM `admin_login` WHERE `username`='$username' AND `password`='$password'"); 

if(mysqli_num_rows($result) > 0) 
{
    $user = mysqli_fetch_assoc($result);
    // user found           
    $response["success"] = 1;
    $response["uid"] = $user["id"];
    $response["username"] = $user["username"];
    $response["role"] = $user["role"];

    echo json_encode($response);
}

一切都很顺利,当我使用开发人员工具时,我才知道它也给出了正确的响应。 php回复:

{"success":1,"uid":"1","username":"admin","role":"admin"}

我应该在 jQuery SUCCESS 函数中编写什么代码才能获得 php 响应?

【问题讨论】:

标签: php jquery json ajax jsonp


【解决方案1】:

如果您想将 php 抛出的结果输出为您的 ajax 结果,那么

像这样创建一个 div

&lt;div id='yourDiv'&gt;&lt;/div&gt;

然后在成功事件里面

 success:function(data)
{       
('#yourDiv').html(data);
}

注意:

如果你喜欢上课,那么

&lt;div class='yourDiv'&gt;&lt;/div&gt;

替换为

('.yourDiv').html(data);

其他数据:

最好像这样检查你的成功事件中的数据

当你得到这样的回应时

{"success":1,"uid":"1","username":"admin","role":"admin"}

success:function(data)
    {        
     if(data.success==1)
    {
    ('#yourDiv').html('Welcome'+data.username);  //You can append or do anything that you wish
    }
    else
    {
    ('#yourDiv').html.('Fail');
    }
    }

【讨论】:

  • 是的,尝试了同样的方法,但没有工作......我从早上开始就在尝试这个。但是错误。它显示在浏览器开发工具上,但不在网页上
  • 如果您有同名的id,它将无法正常工作。你应该试试课。
  • 在成功事件中你能提醒一下吗?
  • 不,我无法在成功事件中提醒任何事情
  • 至少在$("#LoginForm").submit(function(){ ?
【解决方案2】:

很多 RND 我得到了答案 jQuery 代码:

$("#submit").click(function(){

var myData = $("#LoginForm").serialize();

    $.ajax({
          method: "POST",
          dataType:"jsonp",
          url:"https://akshay.tk/api/login.php?callback=?",
          data: myData,

            success:function(msg)
            {
                alert(msg);                 
            }
        });

        return false;           
}); 

PHP 代码略有变化。

if(mysqli_num_rows($result) > 0) 
            {
                $user = mysqli_fetch_assoc($result);
                // user found           
                $response["success"] = 1;
                $response["uid"] = $user["id"];
                $response["username"] = $user["username"];
                $response["role"] = $user["role"];

                echo $_GET['callback'] . '(' . json_encode($response) . ')';
            }

而且它奏效了。我认为回调函数作为参考或其他东西在内部调用,因此我们可以跨域工作

谢谢

【讨论】:

    猜你喜欢
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多