search

Home  >  Q&A  >  body text

javascript ajax button works fine on http://127.0.0.1 but gets error and fails on http://localhost:8888/xxx/public

I am using laravel 10 php 8.10 for work, here the javascript ajax button is working fine on php artisan serve http://127.0.0.1 but getting error on **MAMP PRO** (localhost:8888) ( An error occurred while processing the request) and does not work on the url http://localhost:8888/xxx/public. The following are snippets of html and js:

Need help solving this problem.

**HTML**

@if($row->soc_extra==1)
                        <a class="updatesocialmediaStatus" id="socialmedia-{{ $row->id }}" socialmedia_id="{{ $row->id }}" href="javascript:void(0)"><i class="fas fa-toggle-on" soc-extra="Active"></i></a>
                    @else
                        <a class="updatesocialmediaStatus" id="page-{{ $row->id }}" socialmedia_id="{{ $row->id }}" style='color:#007bff;' style="color:gray" href="javascript:void(0)"><i class="fas fa-toggle-off" soc-extra="Inactive"></i></a>
                    @endif
                      &nbsp;&nbsp;
                      <a style='color:#007bff;' href="{{ url('admin/social-media/edit/'.$row->id) }}"><i class="fas fa-edit"></i></a>
                        &nbsp;&nbsp;
                        <a style='color:#007bff;' class="confirmDelete" name="socialmedia" title="Delete socialmedia" href="javascript:void(0)" record="socialmedia" recordid="{{ $row->id }}" <?php /*href="{{ url('admin/delete-socialmedia/'.$row['id']) }}"*/ ?>><i class="fas fa-trash"></i></a>
                        &nbsp;&nbsp;
@endforeach


**JS**

<script>
    $(document).on("click", ".updatesocialmediaStatus", function(){
        var soc_extra = $(this).children("i").attr("soc-extra");
        var social_media_id = $(this).attr("socialmedia_id");

        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            type: 'post',
            url: '/admin/update-socialmedia-status',
            data: {soc_extra: soc_extra, social_media_id: social_media_id},
            success:function(resp){
                if (resp['status'] == 0) {
                    $("#socialmedia-" + social_media_id).html("<i class='fas fa-toggle-off' style='color:grey' soc-extra='Inactive'></i>");
                } else if (resp['status'] == 1) {
                    $("#socialmedia-" + social_media_id).html("<i class='fas fa-toggle-on' style='color:#007bff' soc-extra='Active'></i>");
                }
             },
             error:function(xhr, textStatus, errorThrown){
                 console.log(xhr.responseText);
                 alert('在处理请求时出错。');
             }
         });
    });
    </script>

Need help solving this problem.

P粉718165540P粉718165540437 days ago739

reply all(1)I'll reply

  • P粉384366923

    P粉3843669232023-09-22 15:29:36

    Is your MAMP Apache port indeed set to port 8888 and not port 80?

    I've done this a few times (port 8888 for testing, port 80 for deployment) and kept the Ajax calls targeting localhost instead of localhost:8888 or vice versa.

    Alternatively, you may need to explicitly specify the 8888 port in the Ajax call, for example:

    url: 'http://localhost:8888/admin/update-socialmedia-status'

    Hope this helps?

    reply
    0
  • Cancelreply