【问题标题】:How to check if ajax doesn't work?如何检查ajax是否不起作用?
【发布时间】:2015-12-20 12:26:32
【问题描述】:

我不知道这几行代码有什么问题,在我之前的插入函数中它可以工作,但这个不行,请帮我弄清楚,因为它已经花了我一个小时了

my views(touristspot.blade.php)
<?php
foreach($data as $r){
echo "<input type='hidden' id='placeid' value='$r->placeid'>";
}
?>

<textarea id="comment"></textarea> <br>
<button type="submit" id="save" name="save">Comment</button>

<script type="text/javascript">
$("#save").click(function(e){
e.preventDefault();
var placeid = document.getElementById('placeid').value;
var comment = document.getElementById('comment').value;

$.ajax({
ur: "touristspot",
type: "POST",
data: {placeid:placeid,comment: comment},
success: function(data) {
//console.log(data);
}
});
});
</script>


my controller
public function addcomment(){
$details = Input::all();
$comments= new Comments;        
$comments->placeid = Input::get('placeid');
$comments->comment = Input::get('comment');
$comments->save();
return View::make('content.touristspot');
}

my routes
Route::post('touristspot','UserController@addcomment');

【问题讨论】:

  • 不应该是ur:url:
  • Ajax 也有失败和错误的功能,尝试借助这些功能进行调试
  • jqueery .ajax 有一个error 回调,很像success 回调` - 请参阅api.jquery.com/jquery.ajax 的文档
  • 也可以更短:$.post("touristspot", {"placeid":placeid,"comment": comment},function(data) { console.log(data); });
  • 使用 firbug 扩展调试错误原因

标签: javascript php mysql ajax laravel


【解决方案1】:

使用 url 代替你的

 $.ajax({
    url: "touristspot", //Valid URL here
    type: "POST",
    data: {placeid:placeid,comment: comment},
    success: function(data) {
    //console.log(data);
     },
    error: function(er){
       console.error(error)
     }
    })

【讨论】:

  • 它并没有真正插入
  • URL 应该指向 Restful api。我没有看到你在这里使用了有效的 URL。
【解决方案2】:

检查 ajax 响应:

$.ajax({
ur: "touristspot",
type: "POST",
data: {placeid:placeid,comment: comment},
success: function(data) {

// add these to check ajax response is null ..

    if (data == null) {
        alert("I am null");

        } else {
        alert(data);

    }

    if ($.isEmptyObject(data)) {
        alert("I am empty");
    } else {
        alert(data);
    }

},
error: function(er){
   console.error(error)
 }
}) 

});

【讨论】:

  • 请澄清完整的问题!什么不工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-14
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多