2016-06-21 54 views
0

我有一個跨域WCF。我已經在該服務上實施了CORS。如何從JQuery Ajax調用跨域WCF服務參數

如果該方法沒有任何參數,那麼我可以使用JQuery Ajax調用它。

但問題是,如果方法有任何參數,那麼在JQuery Ajax調用之後會發生一些錯誤。

$.support.cors = true; 
    $.ajax({ 
    url: servicePath, 
    crossDomain: true, 
    type: 'POST', 
    async: false, 
    cache: false, 
    data: '{"name": "xyz"}', 
    contentType: "application/json; charset=utf-8", 
    dataType: 'json', 
    processData: true, 
    success: function (response) { 
     alert('s'); 
    }, 
    error: function (error) { 
     alert(error.status + ' : ' + error.statusText); 
    } 
    }) 

Server Side Code

+0

是您的服務的一個webapi?你可以傳遞參數在URL中,m lik yourservce.svc/parameter1/parameter2? –

+0

它不是webapi。這是wcf應用程序。方法類型是'post'。 – Ayan

+0

,因爲您可以在不帶參數的情況下到達您的端點,也許此鏈接可以幫助您:http://stackoverflow.com/questions/3876784/calling-wcf-service-with-jquery-and-parameters –

回答

0

Coulde您提供完整的錯誤消息?什麼是你的wcf web.config文件。 我想你可以添加此屬性與您的服務文件。 如下所示:[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 { [WebGet(ResponseFormat = WebMessageFormat.Json)] public Customer GetCustomer() { return new Customer() { Name = "Pranay", Address = "1 Ahmedabad" }; } }