3

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

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

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

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

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

Thanks

El_Pepino
  • 41
  • 6
  • Refer to this post.http://social.msdn.microsoft.com/Forums/vstudio/en-US/979a1df7-7e04-45d6-92bb-6a9ee614941c/how-to-consume-wcf-service-in-javascript?forum=wcf – Shanker Paudel Jul 02 '14 at 14:35
  • Are you getting this error in browser console? Are you able to get alert "success" ? This error not because of the username I think!.. you still get error if you gave "bpepin" as username correct? – byJeevan Jul 02 '14 at 14:39
  • with the *"another username"*, does it work? is the problem just this username? – Kevin B Jul 02 '14 at 14:50
  • @ShankerPaudel : I saw this post, but it doen't help me – El_Pepino Jul 02 '14 at 14:59
  • @jeekonline I had this error in visual studio debbuger ! No I don't be able to get alert "success", the access denied come before ! In fact, if I use bpepin, the backslash no disappear but with jpadoan it disappears ... Sorry for my bad explanations – El_Pepino Jul 02 '14 at 15:04
  • @KevinB I think the username is the problem but I don't sure. In fact, with jpadoan the backslash disapear after ajax request call, but not with bpepin. I don't know why.. – El_Pepino Jul 02 '14 at 15:06
  • Do you know if '\j' is an escape character?? – El_Pepino Jul 02 '14 at 15:18

1 Answers1

2

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.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • "The actual string in the username property will only contain a single backslash, so that's what will be passed to the service." No the username property will contain 0 backslash – El_Pepino Jul 02 '14 at 14:58
  • @El_Pepino `\j` is an escape sequence, but doesn't have any special meaning. The `j` will be taken as-is and the backslash is discarded. `console.log("\j"); // "j"` – Jonathan Lonowski Jul 02 '14 at 15:31
  • I should clarify my comment: if you have `"CWS\\jpadoan"` in javascript, then your actual string will definitely have a single backslash. If you have it the way you originally do, `"CWS\jpadoan"`, then as @JonathanLonowski pointed out, the backslash will be discarded since `\j` doesn't mean anything. A different name, like `"CWS\tsmith"` will actually represent the string `CWS[tab]smith` because `\t` is a representation of the `tab` character. In other words, you always need to escape your backslashes in javascript. – Joe Enos Jul 02 '14 at 15:45
  • @JonathanLonowski ok, and do you know how can I make to have \j ? – El_Pepino Jul 02 '14 at 15:46
  • @El_Pepino And Jon Enos suggested. You'll have to escape the backslash in *code* to keep it as a character in the *value*: `console.log("\\j".length); // 2` – Jonathan Lonowski Jul 02 '14 at 15:47