jQuery AJAX Responses in ASP.NET MVC

前端 未结 4 801
借酒劲吻你
借酒劲吻你 2021-01-03 03:43

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

4条回答
  •  情书的邮戳
    2021-01-03 04:05

    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');
            }
        }
    

    });

提交回复
热议问题