[英]Read JSON returned by AJAX call
I am new to AJAX. 我是AJAX的新手。 I am submitting my form data into a end point using AJAX.
我正在使用AJAX将表单数据提交到端点。 The endpoint is returning a json string with some response data.
端点正在返回带有一些响应数据的json字符串。 I tried many things but wasn;t able to read the JSON.
我尝试了很多事情,但无法读取JSON。
I can parse the JSON. 我可以解析JSON。 At this point, I want to figure out how I can read the JSON returned by the end point.
此时,我想弄清楚如何读取终点返回的JSON。
My code so that posts to the end pint. 我的代码可以放到最后一品脱。
$('.ent-lead-form form').on('submit', function(e){
e.preventDefault();
var form = this;
if($(this).find('.err').length == 0){
$(this).parent().prepend('<div class="form-mask"></div>');
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: $(this).serialize(),
success: function(data){
var data = JSON.parse(json);
alert(data);
// redirect to success or show thank you
if( $(form).find('input[name=successurl]').length == 1 ){
window.location = $(form).find('input[name=successurl]').val();
} else {
$(form).parent().prepend('<div class="confirm-mask">Thank you for your submission.</div>');
}
// cleanup
$('.form-mask').remove();
},
error: function(data){
// show error
$(form).parent().prepend('<div class="confirm-mask">There was an error processing your request. Please reload the page and try again.</div>');
$('.form-mask').remove();
}
});
}
});
You have an error here .. 您在这里有一个错误..
success: function(data){
var data = JSON.parse(json);
alert(data);
Change this to 更改为
success: function(jsondata){
var data = JSON.parse(jsondata);
console.log(data);
Your "data" will be an object. 您的“数据”将是一个对象。 You can get to your values wuith teh dot notation
您可以将值表示为点表示法
data.value
To add a comment to this: It is better to start using .done()
要对此添加评论:最好开始使用
.done()
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8.
弃用通知:从jQuery 1.8开始,不再使用jqXHR.success(),jqXHR.error()和jqXHR.complete()回调。 To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
要准备将其最终删除的代码,请改用jqXHR.done(),jqXHR.fail()和jqXHR.always()。
http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.