0

I have a problem when I try to change data in my database trough knockout.js using an AJAX 'PUT'.

If I ignore the errors I am getting and look at the data in the database, I can see that the data is changed correctly. This error is one thing, but isn't the issue I am posting this question (whould be nice to know this to). But I am guessing they are related.

The real problem is that it is suppose to result in success and not in error so that the code referres back to the index after saving the modification of the "article"

The Function in a knockout.js script looks like this: (inside the viewmodel)

self.saveArticle = function () {

    var isValid = true;

    if (self.articleErrors().length != 0) {
        self.articleErrors.showAllMessages();
        isValid = false;
    }

    if (isValid) {
        $.ajax({
            type: 'PUT',
            cache: false,
            dataType: 'json',
            url: urlFolder + '/UpdateArticleInformation?id=' + self.article().ID,
            data: JSON.stringify(ko.toJS(self.article())),
            contentType: 'application/json; charset=utf-8',
            async: false,
            success: function (data) {
                window.location.href = urlFolder + '/Index';
            },
            error: function (err) {
                var err = JSON.parse(err.responseText);
                var errors = "";
                for (var key in err) {
                    if (err.hasOwnProperty(key)) {
                        errors += key.replace("profile.", "") + " : " + err[key];
                    }
                }
                $("<div></div>").html(errors).dialog({ modal: true, title: JSON.parse(err.responseText).Message, buttons: { "Ok": function () { $(this).dialog("close"); } } }).show();
            },
            complete: function () {
            }
        });
    }
};

When I debug the .js sourcefile in chrome, I get an error: Uncaught SyntaxError: Unexpected end of input right under the folowing in the .js file:

                var err = JSON.parse(err.responseText);

The full error:

Uncaught SyntaxError: Unexpected end of input Folder.Edit-Article.js:63
self.saveArticle.$.ajax.error Folder.Edit-Article.js:63
x.Callbacks.l jquery-2.0.3.js:2913
x.Callbacks.c.fireWith jquery-2.0.3.js:3025
k jquery-2.0.3.js:7402
x.ajaxTransport.x.support.cors.e.crossDomain.send.t jquery-2.0.3.js:7822
x.ajaxTransport.x.support.cors.e.crossDomain.send jquery-2.0.3.js:7845
x.extend.ajax jquery-2.0.3.js:7301
self.saveArticle Folder.Edit-Article.js:51
(anonymous function) knockout-2.3.0.debug.js:2366
x.event.dispatch jquery-2.0.3.js:4676
x.event.add.y.handle

When I log the variable "err.responseText" is turns out empty ("").

The AJAX call referes to the following peace of code in my controller:

    [System.Web.Http.HttpPut]
    public HttpStatusCodeResult UpdateArticleInformation(int id, Article articleModel)
    {
        if (ModelState.IsValid)
        {
            db.Entry(articleModel).State = EntityState.Modified;
            db.SaveChanges();
        }
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }

As you can see I do a direct change in my database trough entity framework (for testing reasons) and when I log the var "articlemodel" it also shows that the data is correct for the modification.

Can't seem to figure this one out myself...

4

0 回答 0