简体   繁体   中英

consuming wcf web service using jquery ajax

I have created one wcf and is running locally in my machine. In the same project i have created a html page calling this wcf web service using ajax.

 $.ajax({
    type: "POST",
    url: "wbsvc.svc/calendar",
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    processData: false,
    success: function (msg) {
        var data = msg.d;
        console.log(data);
    },
    error: function (msg) {
        console.log(msg);
    }
});

calendar is the method in webservice. Error is

Failed to load resource: the server responded with a status of 400 (Bad Request) in wcf using ajax http://localhost:65029/wbsvc.svc/calendar?callback=jQuery110108707461392041296_1379052209564

What i am missing here.

Interface is

    [OperationContract]
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Model.calenderinfo[] calendar();

jsonp用于crossDomain,只需尝试json

Change the datatype to Json.

dataType: "json"

I think the url for your wcf service is worng. Before sending the request, try to open Chrome Developer tool, or Firbug on Firefox, you can see the Request URL.

Now if your request URL is http://localhost/myproject/wbsvc.svc/calendar then type in http://localhost/myproject/wbsvc.svc in browser , if you are able to see the WSDL page, then your URL is correct. Otherwise you need to give correct url in URL parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM