0

I need to display an alert based on occurrence of string in a JSON response. Here is my code:

$.ajax({
    method: 'POST',
    url: dataString2,
    data: dataString,
    headers: {
        "Authorization": "Basic " + btoa('' + Username + '' + ":" + '' + Password + '')
    },
    success: function(jqXHR) {
        if (jqXHR.responseJSON('ok')) {
            alert('First Real Server Configured');
        }
    },
    statusCode: {
        406 : function() {
            alert('There is an unexpected string in your data.\nFix the error and try again.');
        },
        401 : function() {
            alert('Wrong username or password.');
        }
    },
});
});

Here is the JSON response:

{
     "status":"ok"
}

HTTP response code is "200 OK", and the server is responding with Content-Type:application/json

When using the above code, I get: Uncaught TypeError: undefined is not a function

Any idea?

JJJ
  • 32,902
  • 20
  • 89
  • 102
Udi Dahan
  • 452
  • 6
  • 10

2 Answers2

0

Try

$.ajax({
    method: 'POST',
    url: "/echo/json/",
    data: {json:JSON.stringify({status:"ok"})},
    dataType: "json",
    headers: {
        "Authorization": "Basic " 
        + btoa('' + "Username" + '' + ":" + '' + "Password" + '')
    },
    success: function(data, textStatus, jqXHR) {
        // check `jqXHR.responseJSON` here
        if (jqXHR.responseJSON.status === "ok") {
            alert('First Real Server Configured');
        }
    },
    statusCode: {
        406 : function() {
            alert('There is an unexpected string in your data.'
                 + '\nFix the error and try again.');
        },
        401 : function() {
            alert('Wrong username or password.');
        }
    }
});

jsfiddle http://jsfiddle.net/ca9aa04r/

guest271314
  • 1
  • 15
  • 104
  • 177
  • It seems that when I use string that is different than "ok" (but still is part of the response data), the alert doesn't popup. For instance, my server response is: `{ "status":"err", "message":"Alteon cannot create the specified new entry. An entry with the same specifications already exists." }` When I use: ` success: function(data, textStatus, jqXHR) { // check `jqXHR.responseJSON` here if (jqXHR.responseJSON.status === "already exists") { alert('Real Server Already Exists'); } }, The alert is not displayed. – Udi Dahan Jan 17 '15 at 17:45
  • @UdiDahan If possible, can create stacksnippets / fork jsfiddle http://jsfiddle.net/ca9aa04r/ , with included `js` , to demonstrate ? – guest271314 Jan 17 '15 at 17:48
  • You can see in the link that I used if `jqXHR.responseJSON.data` But I also tried `jqXHR.responseJSON.status` like you wrote, and it didn't work. – Udi Dahan Jan 17 '15 at 18:01
  • See `console` at jsfiddle _"Failed to load resource: the server responded with a status of 404 (Not Found) => fiddle.jshell.net/yym93j7y/show/alteon1.js "_ Can post full `url` of `alteon1.js` ? – guest271314 Jan 17 '15 at 18:26
  • It's currently located locally on my PC, so it is a bit of an issue. It seems that it pops up the first alert, no matter what I'm writing in the jqXHR.responseJSON.data. I wrote jqXHR.responseJSON.data = 'bla', and it still pops up the alert (while I can assure you that there is no "bla" string in the response data). – Udi Dahan Jan 17 '15 at 18:41
  • `=` sets / defines, `===` provides strict comparison . Try including text of `alteon1.js` at jsfiddle – guest271314 Jan 17 '15 at 18:53
  • I included the full alteon1.js and the full HTML here: http://jsfiddle.net/8a8co28k/ Just be advised that I'm not looking for a strict comparison (=== operator) but I actually want to search response contains a string. For instance, if the following response: { "status":"ok" } Contains the string "ok". – Udi Dahan Jan 17 '15 at 19:21
  • Any idea guys? Thanks – Udi Dahan Jan 23 '15 at 08:50
0

I found the solution:

$.ajax({
                                        type: 'POST',
                                        url: dataString2,
                                        data: dataString,
                                        contentType: 'application/json',
                                        headers: {
                                            "Authorization": "Basic " + btoa('' + Username + '' + ":" + '' + Password + '')
                                        },
                                        success: function(data) {
                                                status = data.status;
                                                message = data.message;
                                                if (status === 'ok') {
                                                alert("First Real Server Configured Successfully");
                                                }
                                                if (status === 'err') {
                                                alert(message);
                                                }
                                            },
                                        statusCode: {
                                            406 : function() {
                                                alert('There is an unexpected string in your data.\nFix the error and try again.');
                                            },
                                            401 : function() {
                                                alert('Wrong username or password.');
                                            }
                                            },
                                });

Source: $.jquery ajax returned data (json) displays as 'undefined'

Thank you all for your assistance!

Community
  • 1
  • 1
Udi Dahan
  • 452
  • 6
  • 10