jsonp 跨域调用

出处: http://bbs.csdn.net/topics/390858756
WebService是我们自己的项目,ajax是别人调用,但是我需要给别人写个例子。
WebService是带参数的,我现在用ajax调用后总是error报错,报错内容是空的。我贴下代码

这是WebService的代码
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
using System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.Services;
System.Web.Script.Services;
System.Web.Script.Serialization;
namespace MeierbeiWebDtService
{
///<summary>
///WebService的摘要说明
///</summary>
[WebService(Namespace= "http://tempuri.org/" )]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolBoxItem( false )]
//若要允许使用ASP.NETAJAX从脚本中调用此Web服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService:System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet= true ,ResponseFormat=ResponseFormat.Json)]
string HelloWorld()
{
JavaScriptSerializerjs= new JavaScriptSerializer();
Context.Response.Clear();
Context.Response.ContentType= "application/json" ;
Models.HelloWorldDatadata= Models.HelloWorldData();
data.Message= "HelloWorld" ;
data.Name= "MicJackson" ;
return js.Serialize(data);
}
[WebMethod]
void GetClass( int fenshu)
{
JavaScriptSerializer();
Context.Response.Clear();
;
Models.HelloWorldData();
if (fenshu>90)
{
"优异" ;
"语文" ;
}
else (fenshu>80)
{
"良好" ;
;
}
(fenshu>60)
{
"及格" ;
;
}
else
{
"不及格" ;
;
}
Context.Response.Write(js.Serialize(data));
Context.Response.End();
//returnjs.Serialize(data);
}
}
}

这是我写的HTML代码
XML/HTML code
?
41
< html >
head >
script src = "jquery-1.11.1.min.js" type "text/javascript" ></ script >
</ >
body >
>
$(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",
success:function(json){
alert("success:"+json);
alert("error:"+x.responseText);
complete:function(x){
alert("complete:"+x.responseText);
}
});
});
>
>
>

求助,哪里有问题,为什么得不到WebService返回的json数据啊?还有就是如果我想传参,有什么好点的办法吗?谢谢

额,自己解决了,是因为我的html页面不在同一个域里,没跨域,现在加上jsonp就没问题了
C# code

69
70
71
HttpRequestRequest=HttpContext.Current.Request;
callback=Request[ "jsonp" ];
Context.Response.Write(callback+ "(" +js.Serialize(data)+ ")" );
Context.Response.End();
//returnjs.Serialize(data);
}
}
31
functioncallbackjsp(result){
alert("Message:"+result.Message+",Name:"+result.Name);
}
$(document).ready(function(){
$.ajax({
dataType:"jsonp",
jsonp:"jsonp",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(认为:callback)
jsonpCallback:"callbackjsp",//自定义的jsonp回调函数名称认为jQuery自动生成随机函数
data:{fenshu:93},
success:function(json){
alert("success:"+json.Message);
//alert("error:"+x.responseText);
complete:function(x){
//alert("complete:"+x.responseText);
}
});
});
>
>
>

相关文章

AJAX是一种基于JavaScript和XML的技术,能够使网页实现异步交...
在网页开发中,我们常常需要通过Ajax从后端获取数据并在页面...
在前端开发中,经常需要循环JSON对象数组进行数据操作。使用...
AJAX(Asynchronous JavaScript and XML)是一种用于创建 We...
AJAX技术被广泛应用于现代Web开发,它可以在无需重新加载页面...
Ajax是一种通过JavaScript和HTTP请求交互的技术,可以实现无...