2

I am working with DevExtreme. I am trying receive JSON from web api server using ajax.

function getJSONfunction()
{
    $.ajax({
        url: 'http://localhost:999/api/CheckNotification?machinename=LENOVO-PC',
        type: "Get",
        dataType: 'json',
        success: function (data) {
            alert('ok');
        }
    })

I am expecting received from web api json object like this:

{"Result":"true"}

But problem is, web api is sending object like this:

"{\"Result\":\"true\"}"

And I can't see alert from getJSONfunction().

In web api "Get" function look like this:

public string Get(string machineName)
{
    int NotificationsNumber = NotificationsFunctions.CheckForNotifications(machineName);
    NotificationsResult result = new NotificationsResult();
    if(NotificationsNumber > 0)
    {
        result.Result = "true";
    }else
    {
        result.Result = "false";
    }
    JavaScriptSerializer js = new JavaScriptSerializer();
    string json = js.Serialize(result);
    return json;
}

where "NotificationsResult" is a class

public class NotificationsResult
{
    public string Result { get; set; }
}

And my question is how can I received JSON object from api in correct format?

2
  • Just out of curiosity and to possibly help other answers, is there any reason why you're not using a 'bool' for the true/false values and instead using 'string'? Commented Feb 18, 2015 at 12:19
  • No, there is no any reasons ;). I will change this into bool variable. Commented Feb 18, 2015 at 12:52

3 Answers 3

3

It seems like your result is getting serialized twice, once by you, and again by the framework.

There is no need to manually serialize your class. If it's serializable, WebApi will serialize it for you once once you return it.

You can do that following instead:

public NotificationsResult Get(string machineName)
{
    int NotificationsNumber = NotificationsFunctions.CheckForNotifications(machineName);
    return new NotificationsResult
    {
        Result = NotificationsNumber > 0 ? "true" : "false";             
    };
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am sure that JSON is wrong, because when I am openning this json for example in notepad, i've got wrong result :/.
2

Simply use JsonResult

public JsonResult Get(string machineName)
    {
        int NotificationsNumber = NotificationsFunctions.CheckForNotifications(machineName);
        NotificationsResult result = new NotificationsResult();
        if(NotificationsNumber > 0)
        {
            result.Result = "true";
        }else
        {
            result.Result = "false";
        }

        return Json(result);
    }

2 Comments

I've got "The name Json does not exist in the current context" error. Am I missing something ?
Include missing assembly
-1

Use jQuery.parseJSON(json) at client side you may get the result as you expected

1 Comment

JQuery does this for you if the data type is correct. See jquery.ajax Section 'Data Types'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.