简体   繁体   English

使用jQuery AJAX消耗WCF Web服务

[英]consuming wcf web service using jquery ajax

I have created one wcf and is running locally in my machine. 我已经创建了一个wcf,并且正在我的计算机中本地运行。 In the same project i have created a html page calling this wcf web service using ajax. 在同一项目中,我创建了一个HTML页面,该页面使用ajax调用此wcf Web服务。

 $.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. 日历是Web服务中的方法。 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. 将数据类型更改为Json。

dataType: "json"

I think the url for your wcf service is worng. 我认为您的wcf服务的网址已破损。 Before sending the request, try to open Chrome Developer tool, or Firbug on Firefox, you can see the Request URL. 发送请求之前,请尝试打开Chrome Developer工具或Firefox上的Firbug,您可以看到请求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. 现在,如果您的请求URL是http://localhost/myproject/wbsvc.svc/calendar则在浏览器中输入http://localhost/myproject/wbsvc.svc ,如果您能够看到WSDL页面,则您的URL为正确。 Otherwise you need to give correct url in URL parameter. 否则,您需要在URL参数中提供正确的url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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