2017-12-15 3 views
-1

ich den Code unten in WCF RESTful-Service habenCORS arbeiten nicht mit jQuery

public EmployeeJSON GetEmployeeJSON(string id) 
    { 
     List<EmployeeJSON> employees = new List<EmployeeJSON>() 
     { 
      new EmployeeJSON() {Name="Sumanth",Id=101,Salary=5000.00 }, 
      new EmployeeJSON() {Name="Ehsan",Id=102,Salary=6000.00 }, 
     }; 

     var Employee = (from x in employees 
         where x.Id.ToString() == id 
         select x); 


     return Employee.FirstOrDefault() as EmployeeJSON; 
    } 

Die unten ist die Erklärung des REST-konformen Service-

[OperationContract] 
[WebInvoke(Method = "GET", UriTemplate = "GetJson/{id}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] 
EmployeeJSON GetEmployeeJSON(string id); 

Wenn ich führen Sie die unten URL im Browser

http://localhost:1249/Service1.svc/GetJson/101 

ich das Ergebnis als unter

{"GetEmployeeJSONResult":{"Id":101,"Name":"Sumanth","Salary":5000}} 

Aber wenn ich von jQuery nenne, wird der Code geht Block auf Fehler und zeigt leere Nachricht

$(document).ready(function() { 
    }); 

    var GetJson = function() { 
     $.ajax({ 
      type: "GET", 
      url: "http://localhost:1249/Service1.svc/GetJson/101", 
      contentType: "application/json;charset=utf-8", 
      dataType: "json", 
      success: function (data) { 
       var result = data.GetEmployeeJSONResult; 
       var id = result.Id; 
       var name = result.Name; 
       var salary = result.Salary; 
       $('#jsonData').html(''); 
       $('#jsonData').append('<table border="1"><tr><th>Employee Id</th><th>Name</th><th>Salary</th></tr><tr><td>' + id + '</td><td>' + name + '</td><td>' + salary + '</td></tr></table>'); 

      }, 
      error: function (xhr) { 
       alert(xhr.responseText); 
      } 
     }); 
    } 

Der unter dem Code, ich in der web.config von WCF REST-konformer Service-

platziert
<system.webServer> 
<httpProtocol> 
    <customHeaders> 
    <add name="Access-Control-Allow-Origin" value="*" /> 
    </customHeaders> 
</httpProtocol> 
</system.webServer> 

Was fehlt mir hier?

+0

Kann Ihr vielleicht auch noch die kompletten Daten die 'xhr'-Variable (von Ihrem Fehlerblock)? –

+0

Bitte konsultieren Sie die Spezifikation und die Hunderte von Artikeln zu diesem Thema. 'Access-Control-Allow-Origin' ist nur ein Header, den Sie einbinden müssen, normalerweise gibt es auch andere. –

+0

Öffnen Sie die Browser-Konsole und sehen Sie, was genau die Anfrage und die Antwort sind. Meine wilde Vermutung wäre Antwort wird 405 Methode nicht erlaubt. – Evk

Antwort

1

Konfigurieren Sie Ihren Web-API CORS zu verwenden, indem nächsten Zeilen von Code in Ihre Startup-Klasse hinzufügen:

app.UseCors(CorsOptions.AllowAll); 
var config = new HttpConfiguration(); 
var enableCors = new EnableCorsAttribute("*", "*", "*"); 
config.EnableCors(enableCors); 

* wo app ist IAppBuilder und config ist HttpConfiguration

+0

Ist es möglich, so in der web.config-Datei ' '. –

+0

Für mich Einstellung in web.config Datei als Kommentar oben funktioniert nicht. –

+0

Sie müssen WEB API konfigurieren, das Hinzufügen von Header-Attribut ist nicht genug. –