-1

I'm trying to validate my form and I have verified that the array is being sent back to the function but if statement is not working. Again I have looked at the response being sent back and it is set

it is the ifstatement if( test != "false")

json response

{"error":"true","id":62}    

default.js

$(".create").click(function (event) {
        event.preventDefault();
        $("#create-page").dialog({
            buttons: {
                "Create": function () {
                    var ed = tinyMCE.get('c_page_content');
                    var page_headline = $("#page_headline").val();
                    var page_title = $("#page_title").val();
                    var description = $("#description").val();
                    var keywords = $("#keywords").val();
                    var page_content = ed.getContent();
                    var id = $("#id").val();
                    var addItem = $('div[id="contentBox"]:last');
                    $.post("submit", {
                        page_headline: page_headline,
                        page_title: page_title,
                        description: description,
                        keywords: keywords,
                        id: id,
                        page_content: page_content
                    }, function (result) {
                            var test = result.error;
                        if (test != "false") {
                            addItem.append('<div class="contentBox ui-widget-content"><div class="cHeader">' + page_title + '</div><div class="cOption"><a href="create" class="edit" id="edit" rel="' + result.id + '">Edit</a></div><div class="cOption"><a href="json_del" class="delete" id="delete" rel="' + result.id + '">Delete</a></div></div>');
                            $(this).dialog("close");
                            alert("Page created successfully!");
                        } else {
                            $("#title_error").val(result.page_title);
                            $("#content_error").val(result.page_content);
                            if(result.page_title !== "")
                            {
                                    $( 'p[id=title_error]' ).show();
                            }
                            if(result.page_content !== "")
                            {
                                    $( 'p[id=title_error]' ).show();
                            }
                        }
                    });

                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
        $("#create-page").dialog("open");
    });
dxn000
  • 1
  • 2
  • I have also tried to change the variable being passed back to an integer of 0 and the if statement will read if (test < 0) . still no luck – dxn000 May 20 '14 at 00:08
  • What output do you get currently? `Page created successfully` or does an error show? – 000 May 20 '14 at 00:12
  • What does `console.log(response)` say? Did jQuery successfully recognise your response as JSON and parse it? – Bergi May 20 '14 at 00:12
  • 1
    You should use boolean values `true`/`false` instead of string literals. – Bergi May 20 '14 at 00:14
  • Also, generally better to use === (or !==) instead of ==. See here: http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – RobM May 20 '14 at 00:16
  • console output is {"error":true,"id":67} default.js:96 Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'close' jquery.min.js:2 – dxn000 May 20 '14 at 00:30
  • $(this) was being lost when I went into post. But thanks for the help guys. Sorry I'm new to javascript in general so this is a whole new ball game for me. Thanks for the down vote :) buttons: { "Create": function () { var $this = $(this); – dxn000 May 20 '14 at 00:42

4 Answers4

2

Your result may still be a JSON string instead of an object.

Try outputting the result to the console to see what you get:

function (result) {

    console.log(result);

    ...

If it's outputting a string, that means it needs to be converted to a JavaScript object. This can be done with:

var resultObject = $.parseJSON(result);

or you can tell jQuery to automatically convert it to an object by passing a "json" datatype to your post function (see the dataType parameter at http://api.jquery.com/jquery.post/).

gregnr
  • 1,222
  • 8
  • 11
1

Try using :

 var obj = $.parseJSON(result);

to parse the result String to JSON object.

And even better try using jQuery.getJSON method instead of $.post

Runcorn
  • 5,144
  • 5
  • 34
  • 52
0

Parse the json string to an object before you use it:

var test = $.parseJSON(result);
if(test.error != "false") {
    //...
}

Also you may use:

$.post( "...", { ... }, function(result) {
    if(result.error != "false")
}, "json");
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • `$.post` should do that. The correct way to do this is use `$.ajax` and supply the `dataType:'json'` attribute. – 000 May 20 '14 at 00:13
  • `$.post` won't do it automatically unless you supply data type in the last argument. – The Alpha May 20 '14 at 00:20
0

You need to provide the dataType argument to $.post to tell it to decode the JSON automatically:

$.post("submit", {
        <parameters>
}, function(result) {
    ...
}, "json");

The alternative is for the server script to send a Content-Type: application/json header.

Barmar
  • 741,623
  • 53
  • 500
  • 612