2015-04-28 3 views
6

mit Wenn ich einen JQuery Anruf, erhalte ich eine Authentifizierung Antwort fehlgeschlagen:

{ 
    Message: "Authentication failed.", 
    StackTrace: null, 
    ExceptionType: "System.InvalidOperationException" 
} 

jQuery Aufruf:

$(document).ready(function() { 
    //Handle the change event for the drop down list 
    $("#ddRegions").change(function() { 
     //create the ajax request 
     $.ajax({ 
      type: "POST", //HTTP method 
      url: '<%= ResolveUrl("WebForm2.aspx/getLocations")%>', //page/method name 
      data: "{}", //json to represent argument 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (msg) { //handle the callback to handle response     
       //request was successful. so Retrieve the values in the response. 
       alert(msg.toSource()); 
      } 
     }); 
    }); 
}); 

Die Web-Methode:

public static string getLocations() { 
    System.Diagnostics.Debug.WriteLine("Getting Locations."); 
    return "{region:auckland, city:auckland}"; 
} 

ich habe folgendes meine web.config:

<authorization> 
    <allow users="*" /> 
</authorization> 
<authentication mode="None" /> 

Und ich versuchte, die AutoRedirectMode-Off in RouteConfig.cs Einstellung. Dies stoppt den Fehler, aber die Webmethode wird immer noch nicht aufgerufen. Irgendwelche Vorschläge?

+1

Haben Sie versucht, mit $ .support.cors = true; in jquery, um Cross-Herkunft zu ermöglichen, ist dies aus Sicherheitsgründen Browsern vorbehalten? –

+0

Wenn Sie sagen, dass es nie aufgerufen wird, sendet der Browser die Anfrage? Wenn ja, können Sie die Antwort und die Anfrage mit Fiddler oder Google Chrome Developer Tools anzeigen? –

+0

Kann Ihnen das helfen? http://forums.asp.net/t/1975897.aspx?jquery+ajax+calls+to+asp+net+web+methods+authentication+error+ –

Antwort

4

gelöst durch AutoDirectMode auf Aus in App_Start/RouteConfig.cs

settings.AutoRedirectMode = RedirectMode.Off; 

und das Hinzufügen einer Script zum aspx Seite einstellen, dass eine EnablePageMethods auf 'true' hat:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True"> 
    </asp:ScriptManager> 
Verwandte Themen