-1

I know this is very basic question but

Is this a good practice although code works fine?

I'm working on mvc project and i just need to confirm wether it is a good practice to do this or not?

code .cshtml

$(document).ready(function () {
    var serviceURL = '/AjaxTest/FirstAjax';
    if(serviceURL = null ) { alert ("BANG! error")}
    else {
        $.ajax({
                type: "POST",
                url: serviceURL,
                data: param = "",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: successFunc,
                error: errorFunc
        });
    }
}

please share your comments.

4

1 回答 1

2

我是每个函数一个函数的忠实粉丝。如果您正在路由逻辑,则没有理由不在函数中“隐藏”该 ajax 调用:

$(function () {
     var serviceURL = '/AjaxTest/FirstAjax';
     if(serviceURL != null ) { 
         CallServiceAsync(serviceUrl);
     else {
         alert ("BANG! error")} 
     }
}

function CallServiceAsync(serviceUrl) {
   $.ajax({
                type: "POST",
                url: serviceURL,
                data: param = "",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: successFunc,
                error: errorFunc
            });
}

我喜欢隐藏冗长的对象调用以提高代码可读性

于 2013-09-18T16:29:55.967 回答