【问题标题】:when I add b tag in jquery ( in html() ) I get HTTP/1.1 500 Internal Server Error当我在 jquery 中添加 b 标记时(在 html() 中)我得到 HTTP/1.1 500 Internal Server Error
【发布时间】:2016-09-13 08:50:01
【问题描述】:

这是代码

 html
  <a href="#" data-postid="{{ $post->id }}" data-userid="{{ Auth::user()->id }}" class="like-post">
    @if(Auth::user()->likes()->where(['user_id' => Auth::user()->id, 'post_or_comment_id' => $post->id,'isPost' => 1])->first())
      <span class="glyphicon glyphicon-star"></span>like
    @else
      <span class="glyphicon glyphicon-star-empty"></span>like
    @endif
  </a>|

jquery
$('.like-post').on('click',function(event){
    event.preventDefault();
    var userId = event.target.dataset['userid'];
    var postId = event.target.dataset['postid'];

    $.ajax({
        method:'POST',
        url:likeUrl,
        data:{userId:userId, p_or_c:postId, isPost:1, _token:token},
        success:function(msg){
            if(msg['like'] == 0)
                $(event.target).html('<span class="glyphicon glyphicon-star-empty"></span>like');
            else
                $(event.target).html('<span class="glyphicon glyphicon-star"></span>like');

            console.log(msg['like']+'yes');
        }
    })
});

一切正常,但当我改变时:

<span class="glyphicon glyphicon-star"></span>like

到(在 html 和 jquery 文件中):

<span class="glyphicon glyphicon-star"></span><b>like</b>
or
<b><span class="glyphicon glyphicon-star"></span>like</b>

这发生在添加 b 标签之前......一切都很好 screen shot 1 添加b标签后screen shot 2

我收到此错误 HTTP/1.1 500 Internal Server Error 我尝试了很多东西,但似乎 jquery 不接受 html() 中的两个标签,因为我试图将 b 标签更改为 span 标签,并且出现相同的错误并提前感谢

【问题讨论】:

  • 你也可以使用标签代替
  • 我做了同样的事情
  • 请您尝试只放置带有 标记的文本内容,然后检查是什么问题?
  • 这可能与你的 if else 子句有关,尝试强制它为真或假,也许你的类“-empty 没有”加载或什么
  • 我做了 like .....同样的错误

标签: php jquery ajax laravel


【解决方案1】:

试试:

$(event.target).append('<span class="glyphicon glyphicon-star"></span><b>like</b>');

我为你创建了一个 jsFiddle:jsFiddle

【讨论】:

    【解决方案2】:

    试试这个 sn-p,类似于你的例子。

    当您附加 HTML 时,无论您在其中包含多少标签。 HTML 应该是正确的,就是这样。

    $('.like-post').click(function(){
      $(event.target).html("<span class='glyphicon glyphicon-star'></span><b>like</b>");
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a href="#" data-postid="105" data-userid="505" class="like-post">
      Click
    </a>

    【讨论】:

    • 你的意思是我必须将 'like' 更改为 "喜欢”我做了但是同样的错误
    • HTTP/1.1 500 内部服务器错误
    • 在你的代码中 HTTP/1.1 200 OK 但我的是 HTTP/1.1 500 使用 b 标签时出现内部服务器错误
    • 你能评论你的 ajax 调用几分钟,然后用同样的方法试试。它应该可以工作。
    • 我做到了我的意思是每次我点击链接它都会重复自己
    猜你喜欢
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-25
    相关资源
    最近更新 更多