简体   繁体   English

在 ZA5C95B86291EA29902FCBE64458ED127 项目中通过 Javascript 调用 Ajax 中传递 url 的问题

[英]Issue with passing url in Ajax call through Javascript in a Laravel Project

I started learning Laravel a few months ago.几个月前我开始学习 Laravel。 First I developed it on my local machine and later tried to move it to my shared dreamhost hosting.首先,我在本地机器上开发它,后来尝试将它移动到我共享的 Dreamhost 主机上。 While using the ajax calls in the Javascript code in Vue components, I realized that I need to pass full URL for the route.在 Vue 组件中使用 Javascript 代码中的 ajax 调用时,我意识到我需要为路由传递完整的 URL。 Hence I created a global variable in resources/js/app.js因此我在 resources/js/app.js 中创建了一个全局变量

 window.siteURL = (window.location.host.substr(0,9) == '127.0.0.1') ? "http://127.0.0.1:8000/" : "http://example.com/";   

And I created url in my ajax calls like this:我在 ajax 调用中创建了 url,如下所示:

 $.ajax({
        url: siteURL+'client/notesAjax',
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},

        method: 'post',

I am not sure if this was a good scheme, but it worked.我不确定这是否是一个好的方案,但它奏效了。

A few days ago, I registered a domain and tried to run my Laravel project on my AWS EC2 server.几天前,我注册了一个域,并尝试在我的 AWS EC2 服务器上运行我的 Laravel 项目。 After a few hurdles, my Laravel project started running of my domain.经过几个障碍,我的 Laravel 项目开始运行我的域。 However, I realized that I need to empty siteURL for the server on AWS EC2, hence I updated window.siteURL as但是,我意识到我需要为 AWS EC2 上的服务器清空 siteURL,因此我将 window.siteURL 更新为

window.siteURL = (window.location.host.substr(0,9) == '127.0.0.1') ? "http://127.0.0.1:8000/" : "";  

However, some of my ajax calls were still not working.但是,我的一些 ajax 调用仍然无法正常工作。 For example, I had an ajax call on the client/notes page:例如,我在客户端/注释页面上有一个 ajax 调用:

 $.ajax({
      url: siteURL+'client/notesAjax',

But this was failing (everything was still working fine on my local pc and the site running on my shared hosting on Dreamhost. Then I figured out that the url that this call was going to was like this:但这失败了(在我的本地电脑和 Dreamhost 上的共享主机上运行的站点上一切仍然正常。然后我发现这个调用将要调用的 url 是这样的:

http://myawsdomain.ca/client/notes/client/notesAjax

Looks like that as the call is being originated from the client/notes page, it was being prepended to 'client/notesAjax' (siteURL is empty).看起来像来自客户端/注释页面的调用,它被添加到'client/notesAjax'(siteURL 为空)。 As a hack, I created an extra route in routes/web.php作为一个黑客,我在 routes/web.php 中创建了一个额外的路由

Route::post('/client/notesAjax',[clientController::class,'notesAjax'])->name('client.notesAjax');
// on AWS, it looks for the route /client/notes/client/notesAjax (client/notesAjax is called from client/notes page)
Route::post('/client/notes/client/notesAjax',[clientController::class,'notesAjax'])->name('client.notesAjax'); 

I have many other ajax calls that originate from the client/notes page.我还有许多其他来自客户端/注释页面的 ajax 调用。 Using that hack on all those calls may not be appropriate.在所有这些电话上使用该 hack 可能不合适。 What is the best way to handle my situation?处理我的情况的最佳方法是什么? Thanks.谢谢。

unless you are putting your application in a sub-folder, you could've have ommited the base URL for all your HTTP request and everything should have worked without any issue moving from one domain to another.除非您将应用程序放在子文件夹中,否则您可能已经为您的所有 HTTP 请求省略了基本 URL ,并且一切都应该可以正常工作,没有任何问题从一个域移动到另一个域。

so if your route end-point is something like /client/notesAjax因此,如果您的路由端点类似于/client/notesAjax

your http request should also have been like that您的 http 请求也应该是这样的

$.ajax({
        url: '/client/notesAjax',
        .
        .

another thing you could do is define APP_URL in your .env configuration like APP_URL=https://mydomain.me您可以做的另一件事是在.env配置中定义APP_URL ,例如APP_URL=https://mydomain.me

then append that value of config variable in your main index file, something like然后 append 主索引文件中配置变量的值,例如

@php
$config = [
  'baseUrl' => config('app.url'),
];
@endphp

<script>
    window.config = @json($config);
</script>

then use it for all your http request然后将其用于您的所有 http 请求

$.ajax({
        url: `${window.config.baseUrl}/client/notesAjax`,

I changed my ajax call from我将 ajax 电话从

$.ajax({
  url: siteURL+'client/notesAjax',

to

$.ajax({
  url: '/client/notesAjax',

and everything started working fine.一切开始正常。

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

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