404 错误 ajax,代码适用于直接 php / html

发布于 2024-09-14 11:21:56 字数 2513 浏览 2 评论 0原文

所以我的代码本身工作得很好,但我试图在其中添加一些ajax。我正在使用 CI 来进行一些提升。我不明白如果代码在没有ajax的情况下工作正常,为什么会给出404。

这是形式:

 <div class="divider" id="contact">
    <p class = "header"><a id="contactheader" name="gocontact">Contact</a></p>
    <div id = "contactform">
     <form method = "post" id="contactform" action="<?php site_url()?>index.php/home/sendemail">
      <div id ="formtitles">
          <p class = "info">You:</p>
          <p class = "info">Me:</p>
          <p class = "info">Subject:</p>
          <p class = "info">Body:</p>
          <input id = "submit" type="submit" value="Send" />
      </div>
      <div id ="formfields">
       <input id="you" type="text" name="you" /><br/>
       <p class = "info">@gmail.com</p>
       <input id ="subject" type="text" name="subject" /><br/>
       <textarea id = "contactbody"></textarea>
      </div>
     </form>
</div>
</div>

.js

    $(document).ready(function() {

 $('#submit').click(function(){

  var contactformdata = {
   you: $('#you').val(),
   subject: $('#subject').val(),
   message: $('#message').val(),
   }

  console.log(you);

  $.ajax({
   url: "trenthauck.com/index.php/home/sendemail",
   type: 'POST',
   data: contactformdata,
   success: function(){
     $('#contactheader').replaceWith("<p class='header'>Thanks</p>");
     $('#contactform').remove();
     $('#contactlink').remove();
     $(document).scrollTop(25);
   }
  });

  return false;
 });
       });

最后是控制器:

<!--
Name: Trent Hauck
Date: INSERT
File: INSERT
Desc: INSERT
-->

<?php



 class Home extends Controller{

  function index(){

   $this->load->view('home_view');
   return true;
  }

  function sendemail(){
   $to = "[email protected]";
   $from = $this->input->post('you');
   $subject = $this->input->post('subject');
   $message = $this->input->post('contactbody');
   $message = wordwrap($message, 75);

   $tosend = "From: " . $from . "\nMessage: " . $message;

   mail($to, $subject, $tosend);

   $this->index();

  }


 }

附带问题,假设您仍在阅读。无论如何,是否可以在 .js 中执行 CI 中的 site_url() 之类的操作,这样我就不必调用整个过程。谢谢

so my code works fine by itself, but I'm trying to add some ajax into it. I'm using CI, to do some of the lifting. I don't understand why it's giving a 404 if the code works fine without ajax.

Here is the form:

 <div class="divider" id="contact">
    <p class = "header"><a id="contactheader" name="gocontact">Contact</a></p>
    <div id = "contactform">
     <form method = "post" id="contactform" action="<?php site_url()?>index.php/home/sendemail">
      <div id ="formtitles">
          <p class = "info">You:</p>
          <p class = "info">Me:</p>
          <p class = "info">Subject:</p>
          <p class = "info">Body:</p>
          <input id = "submit" type="submit" value="Send" />
      </div>
      <div id ="formfields">
       <input id="you" type="text" name="you" /><br/>
       <p class = "info">@gmail.com</p>
       <input id ="subject" type="text" name="subject" /><br/>
       <textarea id = "contactbody"></textarea>
      </div>
     </form>
</div>
</div>

The .js

    $(document).ready(function() {

 $('#submit').click(function(){

  var contactformdata = {
   you: $('#you').val(),
   subject: $('#subject').val(),
   message: $('#message').val(),
   }

  console.log(you);

  $.ajax({
   url: "trenthauck.com/index.php/home/sendemail",
   type: 'POST',
   data: contactformdata,
   success: function(){
     $('#contactheader').replaceWith("<p class='header'>Thanks</p>");
     $('#contactform').remove();
     $('#contactlink').remove();
     $(document).scrollTop(25);
   }
  });

  return false;
 });
       });

And finally the controller:

<!--
Name: Trent Hauck
Date: INSERT
File: INSERT
Desc: INSERT
-->

<?php



 class Home extends Controller{

  function index(){

   $this->load->view('home_view');
   return true;
  }

  function sendemail(){
   $to = "[email protected]";
   $from = $this->input->post('you');
   $subject = $this->input->post('subject');
   $message = $this->input->post('contactbody');
   $message = wordwrap($message, 75);

   $tosend = "From: " . $from . "\nMessage: " . $message;

   mail($to, $subject, $tosend);

   $this->index();

  }


 }

Side question, assuming you're still reading. Is there anyway to do something like site_url() from CI in the .js so I don't have to call the whole thing. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2024-09-21 11:21:56

您忘记了 Ajax url 参数开头的 http://

因此浏览器认为您将其指向 [ site_url]/trenthauck.com/index.php/home/sendemail,这很可能不是您想要的。

You forgot http:// at the beginning of your Ajax url parameter:

So the browser thinks you're pointing it to [site_url]/trenthauck.com/index.php/home/sendemail, which, in all likelihood, isn't what you intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文