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

时间:2010-08-11 04:43:03

标签: php ajax continuous-integration

所以我的代码本身工作正常,但我正在尝试添加一些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 = "trent.hauck@gmail.com";
   $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(),所以我不需要调用整个事情。感谢

1 个答案:

答案 0 :(得分:1)

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

所以浏览器认为你指的是[site_url]/trenthauck.com/index.php/home/sendemail,这很可能不是你想要的。