Pzz's avatar
Level 4

XHR not working?

My first Laravel step is already troubling me. I followed a screencast example of Caleb:

<?php
namespace App\Livewire;
use Livewire\Component;
class HelloWorld extends Component
{
    public $name ='John' ;
    public function render()
    {
        return view('livewire.hello-world');
    }
}

and the livewire/hello-world.blade.php looks like:

<div>
    <input wire:model="name" type="text">
    Hi {{  $name }}
</div>

When I run this I see the page, with the "Hi John" , I see the input field, but whatever my input is, the name isn't changing. I tried it with Brave and FireFox on Fedora 39, Laravel 10.23, Livewire 3.05. The XHR in the developmenttools is also showing nothing. As a test I tried https://www.w3schools.com/xml/xml_http.asp and this works, and also XHR-output.

Should I enable this somewhere in Laravel to get this working? Livewire does seem to work because the page is loading.

Lary gave me some hints already but that didn't work (or I misinterpreted them...)

Thanks.

0 likes
3 replies
LaryAI's avatar
Level 58

It seems like the XHR (XMLHttpRequest) requests are not working in your Laravel Livewire application. Here are a few steps you can take to troubleshoot and resolve the issue:

  1. Make sure you have the necessary JavaScript libraries included in your project. Livewire relies on Alpine.js, so ensure that you have included it in your HTML file. You can include it by adding the following script tag in your HTML file, preferably before the closing body tag:
<script src="https://cdn.jsdelivr.net/npm/alpinejs@2"></script>
  1. Check if Livewire is properly installed and configured in your Laravel project. Run the following command in your terminal to install Livewire:
composer require livewire/livewire

After installation, make sure you have added the Livewire service provider in your config/app.php file:

'providers' => [
    // Other service providers
    Livewire\LivewireServiceProvider::class,
],
  1. Ensure that you have the necessary Livewire directives in your Blade template. In your livewire/hello-world.blade.php file, make sure you have the wire:model directive correctly applied to your input field. Also, ensure that you have the Livewire component directive (@livewire('hello-world')) in your main Blade template file.

  2. Clear the Laravel application cache. Run the following command in your terminal:

php artisan cache:clear
  1. Check your browser's console for any JavaScript errors. Livewire relies on JavaScript to handle XHR requests, so any errors in your JavaScript code can prevent it from working properly.

If the above steps do not resolve the issue, it's possible that there might be a conflict with other JavaScript libraries or configurations in your project. You can try creating a fresh Laravel project and follow the Livewire documentation to see if the issue persists.

If you're still facing issues, it would be helpful to provide more details about any error messages or logs you're encountering, as well as any additional code or configurations you have in your project.

Pzz's avatar
Level 4

My laravel install is new, right out of the box.

Now I get:

Uncaught TypeError: Alpine.reactive is not a function
    at new Component (livewire.js?id=f41737f6:4190:30)
    at initComponent (livewire.js?id=f41737f6:4250:21)
    at livewire.js?id=f41737f6:6848:26
    at livewire.js?id=f41737f6:1874:58
    at livewire.js?id=f41737f6:908:41
    at Array.forEach (<anonymous>)
    at livewire.js?id=f41737f6:908:26
    at walk (livewire.js?id=f41737f6:831:5)
    at livewire.js?id=f41737f6:906:7
    at deferHandlingDirectives (livewire.js?id=f41737f6:1385:5)
Component @ livewire.js?id=f41737f6:4190
initComponent @ livewire.js?id=f41737f6:4250
(anonymous) @ livewire.js?id=f41737f6:6848
(anonymous) @ livewire.js?id=f41737f6:1874
(anonymous) @ livewire.js?id=f41737f6:908
(anonymous) @ livewire.js?id=f41737f6:908
walk @ livewire.js?id=f41737f6:831
(anonymous) @ livewire.js?id=f41737f6:906
deferHandlingDirectives @ livewire.js?id=f41737f6:1385
initTree @ livewire.js?id=f41737f6:905
(anonymous) @ livewire.js?id=f41737f6:860
start @ livewire.js?id=f41737f6:859
start2 @ livewire.js?id=f41737f6:6864
(anonymous) @ livewire.js?id=f41737f6:7793
livewire.js?id=f41737f6:4272 Uncaught TypeError: Alpine.findClosest is not a function
    at closestComponent (livewire.js?id=f41737f6:4272:31)
    at livewire.js?id=f41737f6:6853:23
    at livewire.js?id=f41737f6:1874:58
    at livewire.js?id=f41737f6:908:41
    at Array.forEach (<anonymous>)
    at livewire.js?id=f41737f6:908:26
    at walk (livewire.js?id=f41737f6:831:5)
    at livewire.js?id=f41737f6:906:7
    at deferHandlingDirectives (livewire.js?id=f41737f6:1385:5)
    at initTree (livewire.js?id=f41737f6:905:5)
Snapey's avatar

Ignore Lary. His answer is bullshit

Do you have Alpine being loaded in bootstrap.js? If do, remove it and re-compile your assets

Please or to participate in this conversation.