简体   繁体   English

用ajax调用特定文件

[英]Call a specific file with ajax

I'm having a problem with ajax, so this is the original call from My_Laravel_5/public/js/init.js 我的ajax有问题,所以这是来自My_Laravel_5 / public / js / init.js的原始调用

$('form#contactForm button.submit').click(function() {

  $('#image-loader').fadeIn();

  var contactName = $('#contactForm #contactName').val();
  var contactEmail = $('#contactForm #contactEmail').val();
  var contactSubject = $('#contactForm #contactSubject').val();
  var contactMessage = $('#contactForm #contactMessage').val();

  var data = 'contactName=' + contactName + '&contactEmail=' + contactEmail +
           '&contactSubject=' + contactSubject + '&contactMessage=' + contactMessage;

  $.ajax({

      type: "POST",
      url: "inc/sendEmail.php",
      data: data,
      success: function(msg) {

        // Message was sent
        if (msg == 'OK') {
           $('#image-loader').fadeOut();
           $('#message-warning').hide();
           $('#contactForm').fadeOut();
           $('#message-success').fadeIn();   
        }
        // There was an error
        else {
           $('#image-loader').fadeOut();
           $('#message-warning').html(msg);
            $('#message-warning').fadeIn();
        }

      }

  });

but I need to call another file wich is located in My_Laravel_5/app/Http/Controllers/sendEmail.php I tried this: 但我需要调用另一个文件,该文件位于My_Laravel_5 / app / Http / Controllers / sendEmail.php中,我尝试了以下操作:

  • url:"../../app/Http/Controllers/sendEmail.php" 网址:“ ../../ app / Http / Controllers / sendEmail.php”
  • url:"../app/Http/Controllers/sendEmail.php" 网址:“ ../ app / Http / Controllers / sendEmail.php”
  • url:"/app/Http/Controllers/sendEmail.php" 网址:“ / app / Http / Controllers / sendEmail.php”

but this isn't working please Help. 但这不起作用,请帮助。

In Laravel, you have a app/Http/routes.php file where you define the bindings between an url, the method of the request (get post delete...), and a controller method. 在Laravel中,您有一个app/Http/routes.php文件,您在其中定义url,请求的方法(获取删除后的内容...)和控制器方法之间的绑定。

You should add to this file: 您应该添加到此文件:

Route::post('/yourAjaxUrl', 'ControllerNamespace\ControllerClassName@method');

Where ControllerNamespace is the namespace of sendEmail.php and ControllerClassName is the name of the class you wrote in this file. 其中ControllerNamespacesendEmail.php的命名空间,而ControllerClassName是您在此文件中编写的类的名称。

And then, in your init.js , change the url parameter of your ajax call object to /yourAjaxUrl 然后,在init.js ,将ajax调用对象的url参数更改为/yourAjaxUrl

Hope this will help you. 希望这会帮助你。

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

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