在ASP.NET MVC jQuery的Ajax响应 [英] jQuery AJAX Responses in ASP.NET MVC

查看:101
本文介绍了在ASP.NET MVC jQuery的Ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

甚至不知道这是正确的方式来标题的问题。我知道我在做我的AJAX调用都错了......这里是我在做什么,现在(我用的方式ASP.NET MVC后端):

Not even sure if this is the right way to title the question. I know I'm making my AJAX calls all wrong... here's what I'm doing right now (I'm using ASP.NET MVC backend by the way):

我用 jQuery.ajax 来发布一些数据,一个动作,这将载入我捕捉在响应视图。它基本上提供一些数据来行动,并采取一些HTML回来了。

I use jQuery.ajax to post some data to an action, which will load a view that I capture in the response. It's basically providing some data to an action, and taking some HTML back.

比方说,我想创建一个有效的Ajax调用。我怎么坐回错误的验证消息?例如,一个Ajax登录表单。如果用户没有通过验证,我想告诉他们,而不是简单地做什么。这...

Let's say I wanted to create an Ajax call that is validated. How do I get back the validation messages on error? For example, an Ajax login form. If the user cannot be validated, I'd like to tell them that... instead of simply doing nothing.

对不起,我幼稚的问题,我只是不知道如何字是正确的,找到的解决方案在谷歌。

Sorry for the naive question, I just don't know how to word it right to find the solutions on Google.

感谢你在前进!

推荐答案

首先,如果你想AJAX调用返回您计划在脚本中使用的数据(并利用我的意思是更复杂的东西,然后显示该数据从你的服务器 DIV ),你应该返回JSON数据,而不是HTML。 JSON实际上是一个JS对象,以便它可以在JS使用它收到的那一刻。

First of all if you want AJAX call to return the data you are planning to use in your scripts (and by use I mean something more complicated then displaying that data in a div), you should probably return JSON data instead of HTML from your server. JSON is actually a JS object so it can be used in JS the moment it is received.

所以,如果你返回数据即。

So if you return the data i.e.

{id: 1, status: 'ok'}

您可以使用像这样使用这些数据。

You can use something like this to use that data.

$.ajax({
    url: "your/script/url",
    type: "POST",
    data: {login : 'foo', pass: 'bar'}, // whatever the data is
    dataType: "json",
    success: function(data){
        if (data.status === 'ok') {
            console.log('success');
        } else {
            console.log('error');
        }
    }

});

这篇关于在ASP.NET MVC jQuery的Ajax响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆