加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – ajax回调无法正常工作

发布时间:2020-12-13 22:49:38 所属栏目:PHP教程 来源:网络整理
导读:我似乎无法得到一个回调来工作,但它得到一个简单的测试响应.任何人都知道为什么它按照我的意愿去做 script type="text/javascript"$(document).ready(function(e) { $("button").click(function() { var msg = $("#txt").val(); $.post( "http://localhost/b
我似乎无法得到一个回调来工作,但它得到一个简单的测试响应.任何人都知道为什么它按照我的意愿去做

<script type="text/javascript">
$(document).ready(function(e) {
    $("button").click(function() {
        var msg = $("#txt").val();
        $.post(
            "http://localhost/bot.php",{msg: msg},function(data,status) {
                $("p").text(data);
            }
        );
    });
});
</script>

PHP,谁能建议一个很好的JavaScript工具来帮助我找到错误?

<?php

class ai
{
    private $msg;

    public function __construct($msg)
    {
        $this->msg = $msg;
    }

    public function respond()
    {
        switch($this->msg)
        {
            case "hi":
                echo "Hello Mr Brown How are You";
                break;
            case "fine":
                echo "Good to hear Did you know Zanda hard Coded me?";
                break;
            case "im fine":
                echo "Good to hear Did you know Zanda hard Coded me?";
                break;
            default:
                echo "??????????????????????????????" .
                     "That means i was extra rush prototype.......i cant answer that";
        }
    }
}

$talk = new ai($_POST['msg']);
$talk->respond();

?>


<div class="box">
<p>text</p>
<textarea id="txt"></textarea>
<button>click</button>
</div>

有html使它尽可能短

解决方法

这似乎是由于同源政策和 MDN documentation

The port number is kept separately by the browser. Any call to the setter,including document.domain = document.domain causes the port number to be overwritten with null. Therefore one can not make company.com:8080 talk to company.com by only setting document.domain = “company.com” in the first. It has to be set in both so that port numbers are both null.

因此,正如您在回复中所说,这就是您获得null的原因

尝试添加数据类型:“jsonp”.它像这样工作.


$(document).ready(function(e) {
    $("button").click(function() {
        var msg = $("#txt").val();
        $.post(
            "http://localhost/bot.php",status) {
                $("p").text(data);
            },dataType:"jsonp"
        );
    });
});

进一步阅读here.

希望有所帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读