0

I'm getting the error

**POST http://localhost:34169/createNew.aspx.cs/Confirm 403 (Forbidden) **

when I am trying to call a CodeBehind function with Jquery AJAX.

My code:

function CallConfirmMethod(str) {
            $.ajax(
            {
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "createNew.aspx.cs/Confirm",
                data: "{'smallPos': " + str + "}",
                success: function (result) { alert("successful!"); }
            });
        }

And the CodeBehind function (doesnt actually do anything, just to test things out):

[System.Web.Services.WebMethod(BufferResponse = false)]
        protected void Confirm(string str) {
            // SKICKA SQL-QUERY
            Response.Write("Funktionen kallas! " + str);
        }

2 Answers2

2

I think your mistake is in url createNew.aspx.cs/Confirm you should change it to createNew.aspx/Confirm. Also it's good article Calling ASP.Net WebMethod using jQuery AJAX

kolesso
  • 336
  • 3
  • 14
  • You are right @kolesso. The server is forbidding the response because it has been requested against the code-behind file, which is not served (Forbidden) by the server. – Atanu Roy Mar 10 '14 at 10:21
  • Changing the URL to createNew.aspx/Confirm gives me error 500 instead (Failed to load resource: the server responded with a status of 500 (Internal Server Error) ), which I guess is a step in the right direction atleast. Thanks! – user3401176 Mar 10 '14 at 10:23
  • Yes. Now the problem is with passing the parameter to `Confirm` method, I think. – Atanu Roy Mar 10 '14 at 10:51
  • Maybe change this data: "{'smallPos': " + str + "}", to data: {"str": str}. – kolesso Mar 10 '14 at 10:57
0

Maybe you get 500, because you haven't close Response

[System.Web.Services.WebMethod(BufferResponse = false)]
protected void Confirm(string str) {
  // SKICKA SQL-QUERY
  Response.Clear();
  Response.ContentType = "application/text";
  Response.StatusCode = 200;
  Response.Write("Funktionen kallas! " + str);
  Response.Flush();
  Response.End();   
}

if you want send response in json this is article AJAX to web method not returning JSON

Community
  • 1
  • 1
kolesso
  • 336
  • 3
  • 14