简体   繁体   中英

Access denied ajax request javascript

I want to make an ajax request in javascript to call WCF service, but I have an error : Access denied.

You can see the code below :

 $("#test").on("click", function () {
    $.ajax({
        type: "GET",
        url: 'http://localhost:1111/Design_Time_Addresses/WCFandEFServiceEnvol/PrestationService/Methode',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        username: "CWS\jpado",
        password: "Password",
        success: function (prestaList) {
            alert("success");
            var prestaPicker = $("#Presta");
            $(prestaList.d).each(function () {
                prestaPicker.html(prestaPicker.html() +
                    "<p>" + this.id_prestation + "<\p>"
                );
            });
        },
        error: onError
    });
});

I think the issue comes from the "username" because when I use debug, antislash \\ (in the username) disappears after calling the ajax request.

I tried to use username: "CWS\\\\jpado", but it doesn't work (the both antislashes stay like that). I don't understand, because with another username (like bpepin for instance) the antislash doesn't disappear..

Edit : Do you know if '\\j' is an escape characters ??

Sorry, I don't know if I well explain. Tell me if you want more informations.

Thanks !

EDIT : I edit adding two pictures

用户名='\\ _'我在ajax请求后得到'j'

with username = '\\j' I get 'j' after ajax request

用户名='\\\\ j'我在ajax请求后得到'\\\\ j' with username = '\\\\j' I get '\\\\j' after ajax request

How can I get '\\j' after ajax request ??

Thanks

You're confusing the escape characters. Backslash is an escape character in both javascript and C#, so your javascript code definitely needs the second backslash to escape the real one:

username: "CWS\\jpadoan"

The actual string in the username property will only contain a single backslash, so that's what will be passed to the service.

When debugging in C#, depending on what you're looking at, you may or may not see the second backslash when looking at the contents of that string. The easiest way to see the "real" string in the C# debugger is to put it in the watch window, then click the little visualizer button to bring up a popup with the real string value.

You've also got another backslash in your success function, which is supposed to be a forward slash:

"<p>" + this.id_prestation + "</p>"

In both cases, your javascript is going to be screwy, may not even compile properly.

Don't know if that's the only problem, but you definitely need to get the backslashes right before anything else will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM