[原]js调用WCF和Web Service

 1 (function ($) {
 2 
 3     var serviceUrl;
 4 
 5     $.fn.serviceinvoke = function (wstype, method, callback, params) {
 6 
 7         // 选择请求服务的类型
 8 
 9         switch (wstype) {
10 
11             case 0:
12 
13                 // 设置为请求WebService 
14 
15                 serviceUrl = '/WebService/WebService.asmx/';
16 
17                 break;
18 
19             case 1:
20 
21                 // 设置为请求WCF
22 
23                 serviceUrl = '/WCFService/WCFService.svc/';
24 
25                 break;
26 
27             default:
28 
29         }
30 
31  
32 
33         $.ajax({
34 
35             type: "post",
36 
37             url: serviceUrl + method,
38 
39             contentType: "application/json;charset=utf-8",
40 
41             data: params,
42 
43             dataType: 'json',
44 
45             timeout: 10000,
46 
47             success: function (data) {
48 
49                 // 如果是Web Service优先执行
50 
51                 if (data.d) {
52 
53                     callback(data.d);
54 
55                 }
56 
57                 else {
58 
59                     callback(data);
60 
61                 }
62 
63             },
64 
65             error: function (XMLHttpRequest, textStatus) {
66 
67                 alert(textStatus);
68 
69             },
70 
71             beforeSend: function (xml) {
72 
73                 if (!params)
74 
75                 xml.setRequestHeader("Content-Type", "application/json;utf-8")
76 
77             },
78 
79             cache: false
80 
81         });
82 
83     };
原文地址:https://www.cnblogs.com/crmhf/p/3823248.html