404 错误 ajax,代码适用于直接 php / html
所以我的代码本身工作得很好,但我试图在其中添加一些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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记了 Ajax
url
参数开头的http://
:因此浏览器认为您将其指向
[ site_url]/trenthauck.com/index.php/home/sendemail
,这很可能不是您想要的。You forgot
http://
at the beginning of your Ajaxurl
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.