jsonp 跨域调用
http://blog.csdn.net/chelen_jak/article/details/51830057
出处:http://bbs.csdn.net/topics/390858756
WebService是我们自己的项目,ajax是别人调用,但是我需要给别人写个例子。
WebService是带参数的,我现在用ajax调用后总是error报错,报错内容是空的。我贴下代码
这是WebService的代码
这是我写的html代码
WebService是我们自己的项目,ajax是别人调用,但是我需要给别人写个例子。
WebService是带参数的,我现在用ajax调用后总是error报错,报错内容是空的。我贴下代码
这是WebService的代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Script.Services;using System.Web.Script.Serialization;namespace MeierbeiWebDtService{ /// <summary> /// WebService 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public string HelloWorld() { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/json"; Models.HelloWorldData data = new Models.HelloWorldData(); data.Message = "HelloWorld"; data.Name = "MicJackson"; return js.Serialize(data); } [WebMethod] [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] public void GetClass(int fenshu) { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/json"; Models.HelloWorldData data = new Models.HelloWorldData(); if (fenshu > 90) { data.Message = "优异"; data.Name = "语文"; } else if (fenshu > 80) { data.Message = "良好"; data.Name = "语文"; } else if (fenshu > 60) { data.Message = "及格"; data.Name = "语文"; } else { data.Message = "不及格"; data.Name = "语文"; } Context.Response.Write(js.Serialize(data)); Context.Response.End(); //return js.Serialize(data); } }} |
这是我写的html代码
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<html><head> <script src="jquery-1.11.1.min.js" type="text/javascript"></script></head><body> <script type="text/javascript"> $(document).ready(function(){ $.ajax({ url: "http://localhost:53568/WebService.asmx/GetClass", type: "GET", dataType: "json", data: {fenshu: 23}, success: function(json) { alert("success:"+json); }, error: function(x, e) { alert("error:"+x.responseText); }, complete: function(x) { alert("complete:"+x.responseText); } }); $.ajax({ url: "http://localhost:53568/WebService.asmx/HelloWorld", type: "GET", dataType: "json", success: function(json) { alert("success:"+json); }, error: function(x, e) { alert("error:"+x.responseText); }, complete: function(x) { alert("complete:"+x.responseText); } }); }); </script></body></html> |
求助,哪里有问题,为什么得不到WebService返回的json数据啊?还有就是如果我想传参,有什么好点的办法吗?谢谢
额,自己解决了,是因为我的html页面不在同一个域里,没跨域,现在加上jsonp就没问题了
C# code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Script.Services;using System.Web.Script.Serialization;namespace MeierbeiWebDtService{ /// <summary> /// WebService 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { [WebMethod] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public string HelloWorld() { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/json"; Models.HelloWorldData data = new Models.HelloWorldData(); data.Message = "HelloWorld"; data.Name = "MicJackson"; return js.Serialize(data); } [WebMethod] [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] public void GetClass(int fenshu) { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/json"; Models.HelloWorldData data = new Models.HelloWorldData(); if (fenshu > 90) { data.Message = "优异"; data.Name = "语文"; } else if (fenshu > 80) { data.Message = "良好"; data.Name = "语文"; } else if (fenshu > 60) { data.Message = "及格"; data.Name = "语文"; } else { data.Message = "不及格"; data.Name = "语文"; } HttpRequest Request = HttpContext.Current.Request; string callback = Request["jsonp"]; Context.Response.Write(callback + "(" + js.Serialize(data) + ")"); Context.Response.End(); //return js.Serialize(data); } }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<html><head> <script src="jquery-1.11.1.min.js" type="text/javascript"></script></head><body> <script type="text/javascript"> function callbackjsp(result){ alert("Message:"+result.Message + ",Name:"+result.Name); } $(document).ready(function(){ $.ajax({ url: "http://localhost:53568/WebService.asmx/GetClass", type: "GET", dataType: "jsonp", jsonp: "jsonp", //传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback) jsonpCallback: "callbackjsp", //自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名 data: { fenshu: 93 }, success: function (json) { alert("success:" + json.Message); }, error: function (x, e) { //alert("error:" + x.responseText); }, complete: function (x) { //alert("complete:" + x.responseText); } }); }); </script></body></html> |

浙公网安备 33010602011771号