我希望跨域返回一些JSON,我知道这样做的方法是通过JSONP而不是纯JSON.我正在使用ASP.net MVC所以我正在考虑只是扩展JSONResult类型然后extendig Controller,以便它还实现了一个Jsonp方法.这是最好的解决方法,还是内置的ActionResult可能会更好?
编辑:我继续前进并做到了.仅供参考,我添加了一个新结果:
public class JsonpResult : System.Web.Mvc.JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/javascript";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
// The JavaScriptSerializer type was marked as obsolete prior to .NET Framework 3.5 SP1
#pragma warning disable 0618
HttpRequestBase request = context.HttpContext.Request;
JavaScriptSerializer serializer …Run Code Online (Sandbox Code Playgroud) 我试图用jQuery访问谷歌文档.这是我到目前为止所拥有的:
var token = "my-auth-token";
$.ajax({
url: "http://docs.google.com/feeds/documents/private/full?max-results=1&alt=json",
dataType: 'jsonp',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "GoogleLogin auth=" + token);
},
success: function(data, textStatus, XMLHttpRequest) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
Run Code Online (Sandbox Code Playgroud)
如果我设置dataType为jsonp(来自使用jQuery创建跨域Ajax请求),它不允许我设置标头.如果我遗漏jsonp,我无法提出跨域请求.如果我使用jQuery.getJSON,我不能传入任何标题...
在制作跨域ajax请求时(在jQuery中)有没有办法定义自定义标头?
有点棘手的问题.
我正在开展一个项目,当用户在自助服务终端上查看我们的网站时,我们需要允许收据打印输出.由于与驱动程序和格式相关的原因,我使用Word自动化COM来处理打印收据.我已将此代码包装在本地计算机上运行的Web服务中.
计划是在页面html中将一个简单的jQuery ajax调用放到运行Web服务的本地机器的url中.此ajax调用包含订单的json对象,该对象由Web服务反序列化并打印出来.如果我使用localhost就可以正常工作,但是在生产中我会遇到没有跨域的ajax调用规则.
代理将无法工作,因为网站上运行的代码无法联系运行打印服务的本地Web服务.在浏览网页后,我发现使用JSONP可能是解决方案,但我无法弄清楚如何使其工作.大多数教程都假设您尝试获取一些远程数据而不仅仅是发布数据.打印Web服务返回void.
如何配置我的Web服务(asmx)以使用JSONP以及我的jQuery代码是什么样的?目前它看起来像这样:
function printReceipt(data) {
$.ajax({
type: "POST",
url: "http://192.9.200.165/ContestWebService/Service1.asmx/PrintOrderReceiptJson",
data: data,
dataType: "json",
contentType: "application/json; charset=utf-8",
error: function(xhr, msg) { alert(xhr.statusText); }
});
}
Run Code Online (Sandbox Code Playgroud)
任何更简单的JSONP替代方案,或者我可能没有考虑的任何其他可能的解决方案也会有所帮助.
请查看下面的代码并帮助我弄清楚我的Web服务代码中出了什么问题.我想建立一个可以使用JSONP使用的asp.net Web服务.我在客户端使用Jquery来访问该站点.即使在设置了适当的属性后,我的Web服务仍然会发出xml,因为aynch调用失败了.
网络服务
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat= ResponseFormat.Json, XmlSerializeString=false, UseHttpGet=true)]
public string HelloWorld(int id, string __callback) {
return __callback + "({message: 'Hello World'})";
}
}
Run Code Online (Sandbox Code Playgroud)
Web服务响应:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">test({message: 'Hello World'})</string>
Run Code Online (Sandbox Code Playgroud)
Web.Config中:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> …Run Code Online (Sandbox Code Playgroud) jquery ×3
jsonp ×3
ajax ×2
asp.net ×2
json ×2
asp.net-mvc ×1
cross-domain ×1
header ×1
javascript ×1
web-services ×1