最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery Ajax and Json : How to check if undefined - Stack Overflow

programmeradmin1浏览0评论

I got this Jquery code :

$.ajax({
    url: "?module=gestionApplication&action=getTests&scenario="+encodeURI(scenario)+"&application="+$application,
    dataType:'json',
    success: function( data ) {
        $.each(data, function(i, item) {
            $("#tests tbody").append($tr+"<td title='"+item.DESCRIPTION+"'>"+item.ID+"</td>" +
            "<td>"+
                "Header : "+item.HEADER + '<br/>' +
                "Méthode : "+item.METHODE +  '<br/>' +
                "PostBody : "+item.POSTBODY +  '<br/>' +
                "URL : "+item.URL +  '<br/>' +
                "ParseReponse : "+item.PARSEREPONSE +  '<br/>' +
            "</td>" +

So i got a JSON response from my server, but not all fields are full. Sometimes item.HEADER or item.METHODE can not be defined, so I get "undefined" text in my table. Problem is, I'm French and I would like different text and not this 'undefined'.

So how can I test if the variable is defined or not? Or even better, is it possible to change this 'undefined' text to different text in case the variable is not defined?

I got this Jquery code :

$.ajax({
    url: "?module=gestionApplication&action=getTests&scenario="+encodeURI(scenario)+"&application="+$application,
    dataType:'json',
    success: function( data ) {
        $.each(data, function(i, item) {
            $("#tests tbody").append($tr+"<td title='"+item.DESCRIPTION+"'>"+item.ID+"</td>" +
            "<td>"+
                "Header : "+item.HEADER + '<br/>' +
                "Méthode : "+item.METHODE +  '<br/>' +
                "PostBody : "+item.POSTBODY +  '<br/>' +
                "URL : "+item.URL +  '<br/>' +
                "ParseReponse : "+item.PARSEREPONSE +  '<br/>' +
            "</td>" +

So i got a JSON response from my server, but not all fields are full. Sometimes item.HEADER or item.METHODE can not be defined, so I get "undefined" text in my table. Problem is, I'm French and I would like different text and not this 'undefined'.

So how can I test if the variable is defined or not? Or even better, is it possible to change this 'undefined' text to different text in case the variable is not defined?

Share Improve this question edited Aug 7, 2012 at 2:25 Crippledsmurf 4,0121 gold badge33 silver badges51 bronze badges asked Aug 6, 2012 at 13:57 timmalostimmalos 5423 gold badges10 silver badges25 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

You can do a quick conditional / logical-OR check within your concat:

"Header : " + (item.HEADER || '') + '<br/>' +

so now, if item.HEADER is undefined we will encounter the empty string instead. Of course you could also use a more expressive string like "empty" or whatnot.

if (typeof variable == "undefined")
{
    // variable is undefined
}

Use a ternary operator... ( test ? do if true : do if false )

...+( item.HEADER ? item.HEADER : "something french" )+...

easily done like so:

if (item.HEADER === undefined) {
    item.HEADER = 'indéfini';
}

// or 

if (typeof item.HEADER === 'undefined') {
    item.HEADER = 'indéfini';
}
发布评论

评论列表(0)

  1. 暂无评论