1

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>";
0

2 Answers 2

2
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"})"
Sign up to request clarification or add additional context in comments.

2 Comments

I couldn't make your style, whenever I use ' browser truns that to " ; I paste my button creator script to question too.
still getting same error this is output ; onclick="genericAjax('/Admin/LoginAsUser.aspx/LoginAs',{"UserUId":"854c46fc-6e50-4574-a103-16e16f24bf38"})"
0

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>

Comments

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.