我已经编写了一个ajax调用来调用用于发布数据的API,并在成功响应后获取,并以各自形式的div标签显示它,但是它给了我完整的json {"code":200,"status":"OK","msg":"Login Success"}
,但是我只想显示“味精”部分。我怎样才能做到这一点?我正在Django模板上执行此操作。
我试图做res['msg']
(Python键,有价值的东西),但不起作用
<script>
$(document).ready(function(){
var myForm = $('.my-login-form')
myForm.submit(function(e){
e.preventDefault()
console.log('hi')
var formData = $(this).serialize()
$.ajax({
method: "POST",
url: '/login',
data: formData,
success: function(res) {
$('#login-data').html(res);
},
})
})
})
</script>
<form class="my-login-form" action="/login" method="post">
{% csrf_token %}
<div id="login-data"></div>
<div class="field-wrap">
<input type="email" name="login-email" required autocomplete="off" placeholder="Email Id"/>
</div>
<div class="field-wrap">
<input type="password" name="login-password" required autocomplete="off" placeholder="Password">
</div>
<button class="button button-block"/>Login</button>
<div class="forgot"><a class="user-form-toggle" href="#forgot">Forgot Password?</a></div>
</form>
答案 0 :(得分:2)
这是解决方案:
<script>
$(document).ready(function(){
var myForm = $('.my-login-form')
myForm.submit(function(e){
e.preventDefault()
console.log('hi')
var formData = $(this).serialize()
$.ajax({
method: "POST",
url: '/login',
data: formData,
success: function(res) {
var rd = $.parseJSON(res);
$('#login-data').html(rd.msg);
},
})
})
})
</script>
希望它对您有用