Ajax console.log不显示数据 [英] Ajax console.log does not display data
本文介绍了Ajax console.log不显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JavaScript编写联系人脚本。我的问题是console.log不在控制台中显示数据。我不知道为什么会这样。请帮忙。下面的脚本。
JScript
$(' form.ajax')。on(' 提交',功能(){
var = $( this ),
url = that.attr(' action'),
type = that.attr(' method') ,
data = {};
that.find(' [name] ')。each( function (index,value){
var that = $( this ),
name = that.attr(' 名称),
value = that.val();
data [name] = value;
});
$ .ajax({
url:url,
type:type,
data:data,
success:功能(响应){
console .log(响应);
}
});
return false ;
});
PHP
< ;?php
if(isset($ _ POST [' name'] ,$ _POST [' email'],$ _ POST [ ' message'])){
print_r($ _ POST);
}
?>
和HTML
< !DOCTYPE html >
< html >
< body >
< 表格 action = contact.php methond = POST class = ajax >
< div >
< 输入 类型 = text name = 名称 占位符 = 您的姓名 >
< / div >
< div >
< input type = email name = 电子邮件 占位符 = 您的电子邮件 >
< / div > ;
< div >
< textarea 名称 = 消息 占位符 = 消息 > < / textarea >
< / div >
< span class =code-keyword>< input type = 提交 value = 发送 >
< / form >
< script type = text / javascript src = https://ajax.googleapis.com /ajax/libs/jquery/3.2.1/jquery.min.js\"> < / script >
< script 类型 = text / javascript src =' main.js' > < / script >
< / body >
< / html >
我尝试了什么:
更改
成功: function (回复){
}
to
。done( function (){
});
解决方案
(' form.ajax')。on(' submit', function (){
var that =
( this ),
url = that.attr(' action'),
type = that.attr (' method'),
data = {};
that.find(' [name]')。每个(< span class =code-keyword> function (index,value){
var that =
< blockquote>( this ),
name = that.attr(' name'),
value = that.val();
data [name] = value;
});
I am writing a contact script using JavaScript. My problem is that console.log does not display data in the console. I do not know why this is happening. Please help. Below scripts.
JScript
$('form.ajax').on('submit', function(){
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value){
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response){
console.log(response);
}
});
return false;
});
PHP
<?php
if(isset($_POST['name'], $_POST['email'], $_POST['message'])){
print_r($_POST);
}
?>
And HTML
<!DOCTYPE html>
<html>
<body>
<form action="contact.php" methond="POST" class="ajax">
<div>
<input type="text" name="name" placeholder="Your name">
</div>
<div>
<input type="email" name="email" placeholder="Your email">
</div>
<div>
<textarea name="message" placeholder="Message"></textarea>
</div>
<input type="submit" value="Send">
</form>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src='main.js'></script>
</body>
</html>
What I have tried:
Change
success: function(response){
}
to
.done(function() {
});
解决方案
('form.ajax').on('submit', function(){ var that =
(this), url = that.attr('action'), type = that.attr('method'), data = {}; that.find('[name]').each(function(index, value){ var that =
(this), name = that.attr('name'), value = that.val(); data[name] = value; });
这篇关于Ajax console.log不显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文