Mert
Mert

Reputation: 6572

Invalid JSON primitive as javascript parameter

I am having this error. I always having problem with this quotes :(

<a class="btn-xs btn-info" title="Login As User" onclick="genericAjax("/Admin/LoginAsUser.aspx/LoginAs",{"UserUId":"854c46fc-6e50-4574-a103-16e16f24bf38"})" href="#">Login</a>

function genericAjax(url, data) {
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { }
    });
}


    [WebMethod]
    public static object LoginAs(string UserUId)
    {
        //somecode
    }

Button Creator Javascript

 var param = '{';
                    for (t in CustomButtonJson[i].param)
                        param += '"' + CustomButtonJson[i].param[t]["Key"] + '":"' + rowData[CustomButtonJson[i].param[t]["Value"]] + '",';

                    param = param.substring(0, param.length - 1) + '}';

                    var ajax = 'genericAjax("' + CustomButtonJson[i].url + '/' + CustomButtonJson[i].ajaxMethod + '",' + param + ')';


                    button += 
                        "<a class='btn-xs btn-info' href='#' onclick=" + ajax + " " +
                            "title='" + CustomButtonJson[i].title + "'>" + CustomButtonJson[i].name + "</a>";

Upvotes: 1

Views: 170

Answers (2)

user23708
user23708

Reputation: 44

The answer is :

<form id="form1" runat="server">
    <div>
        <a class="btn-xs btn-info" title="Login As User" id="btnLogin" href="#">Login</a>
        <a class="btn-xs btn-info" title="Login As User" onclick="genericAjax('/Default.aspx/LoginAs', '{&quot;UserUId&quot;:&quot;bbbb&quot;}')" href="#">Login</a>

    </div>
    <script>
        $(document).ready(function () {
            $("#btnLogin").click(function () {
                genericAjax('/Default.aspx/LoginAs', '{"UserUId":"aaaa"}');
            });
        });
        function genericAjax(url, data) {
            $.ajax({
                type: "POST",
                url: url,
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) { alert(data.d); }
            });
            return false;
        }
    </script>
</form>

Upvotes: 0

user1017882
user1017882

Reputation:

onclick='genericAjax("/Admin/LoginAsUser.aspx/LoginAs",{"UserUId":"854c46fc-6e50-4574-a103-16e16f24bf38"})'

You're using double quotes for surrounding the onclick event, but also to pass in parameters to genericAjax so there's no way for JS to distinguish between the two uses here.

Instead, wrap the whole thing with a different set of characters (single quotes) (I think you also have the option to escape the quotes here but this is the simplest solution I can see).

Funnily enough - the colouring of StackOverflow's code snippets here illustrate perfectly what I'm explaining. Compare the colours to my proposed solution above to the following (yours) and you can see what's happening:

onclick="genericAjax("/Admin/LoginAsUser.aspx/LoginAs",{"UserUId":"854c46fc-6e50-4574-a103-16e16f24bf38"})"

Upvotes: 2

Related Questions