简体   繁体   English

Laravel 4 JavaScript Ajax请求

[英]Laravel 4 javascript ajax request

I performed and Ajax request when a button is being clicked, to make a get request and update a div,but my page doesn't seem to update at all.but using the debugging tools i realised that: 我在单击按钮时执行了Ajax请求,以进行get请求并更新了div,但是我的页面似乎根本没有更新。但是使用调试工具,我意识到:

it gives error: Cannot set property 'innerHTML' of null 

But when i save my xmlhttp.responseText to a variable and i alert it alert this: 但是,当我将我的xmlhttp.responseText保存到一个变量中并且我警告它时,它会发出警报:

<div>

This is a new Ajax Message !! Thanks
</div>

which is the content of msg.blage.php that am requesting for.Though am new to using ajax and laravel. 这是要求的msg.blage.php的内容。尽管这对于使用ajax和laravel是新的。

Below here is the code: 下面是代码:

register.blade.php register.blade.php

<button id="btn" onclick="makerequest('{{route('msg')}}','succesMessage')">Update!!</button>
<div id="successMessage"></div>

msg.blade.php msg.blade.php

<div>
This is a new Ajax Message !! Thanks
</div>

script.js script.js

function makerequest(serverPage, objID) {
var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  var data = xmlhttp.responseText;
  alert(data);
obj.innerHTML=data;
}
}
xmlhttp.send(null);
}

routes.js routes.js

Route::get('/msg',['as' => 'msg', 'uses' => 'HomeController@msg']);

HomeContoller.php HomeContoller.php

public function msg()
    {
        return View::make('msg');
    }

The error shows on line "sortable.js:14"; 错误显示在“ sortable.js:14”行上;

and that i think is the second each() 我认为这是第二个each()

<button id="btn" onclick="makerequest('{{route('msg')}}','succesMessage')">Update!!</button>
<div id="successMessage"></div>

You have a typo in the first line, succesMessage instead of successMessage . 第一行中有一个错字,即succesMessage而不是successMessage It's trying and failing to find an element with the ID succesMessage as a result. 结果是尝试并未能找到ID为succesMessage的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM