发布跨域JSON到ASP.NET使用jQuery [英] Posting cross-domain JSON to ASP.NET with jQuery

查看:105
本文介绍了发布跨域JSON到ASP.NET使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GOT样的一个棘手的问题。

我工作的一个项目,我们需要允许打印回执当用户签出我们的网站在展台。对于与驱动程序和格式方面的原因,我使用与Word COM自动化处理打印出收据。我一直包在一个本地机器上运行的Web服务这code了。

该计划是把一个简单的jQuery Ajax调用HTML网页运行Web服务的本地计算机的URL。这AJAX调用包含的顺序,这是由网络服务反序列化和打印出来的JSON对象。工作正常,如果我使用localhost,但在生产中我不会跨域Ajax调用规则相抵触。

一个代理将无法工作,因为code在网站上还无法与运行的打印服务的本地Web服务。在网络上闲逛之后,我发现,使用JSONP可能是一个解决方案,但我想不出如何使它发挥作用。大多数教程假设你正在试图获得一些偏远的数据,而不是只是简单发布的数据。打印Web服务返回void。

如何配置我的web服务(ASMX)与JSONP工作,会是什么我的jQuery code样子?目前,它看起来是这样的:

 函数printReceipt(数据){
   $阿贾克斯({
       键入:POST,
       网址:http://192.9.200.165/ContestWebService/Service1.asmx/PrintOrderReceiptJson
       数据:数据,
       数据类型:JSON,
       的contentType:应用/ JSON的;字符集= UTF-8,
       错误:函数(XHR,味精){警报(xhr.statusText); }
    });
}
 

要JSONP,否则我可能还没有考虑将是有益的,以及其他任何可能的解决方案,任何简单的替代品。

解决方案

JSONP只是增加了一个剧本标签部,因此仅限于GET请求。为了配置您的ASMX Web服务来处理JSONP,你需要手动处理序列化:

  [WebMethod的]
[ScriptMethod(UseHttpGet = TRUE,ResponseFormat = ResponseFormat.Json)
公共字符串美孚()
{
    VAR JSON =新JavaScriptSerializer()。序列化(新
    {
        为prop1 =部分产权,
    });
    字符串jsoncallback = HttpContext.Current.Request [jsoncallback];
    返回的String.Format({0}({1}),jsoncallback,JSON);
}
 

和客户端:

<$p$p><$c$c>$.getJSON("http://192.9.200.165/ContestWebService/Service1.asmx/PrintOrderReceiptJson?jsoncallback=?",     功能(数据){         警报(数据);     });

有关跨域AJAX调用另一种方法是使用Flash

Got kind of a tricky problem.

I'm working on a project where we need to allow receipt printouts when users check out on our site at a kiosk. For reasons relating to drivers and formatting, I am using COM automation with Word to handle printing out the receipts. I've wrapped this code up in a web service that runs on a local machine.

The plan was to put a simple jQuery ajax call in the page html to the url of the local machine running the web service. This ajax call contains a json object of the order, which is deserialized by the web service and printed out. Works fine if I use localhost, however in production I will run afoul of no cross domain ajax calls rule.

A proxy will not work because the code running on the website cannot contact the local web service running the printing service. After poking around on the web, I discovered that using JSONP may be a solution to this but I can't figure out how to make it work. Most tutorials assume that you are trying to get some remote data rather than just simply posting data. The printing web service returns void.

How do I configure my web service (asmx) to work with JSONP and what would my jQuery code look like? Currently it looks something like this:

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); }
    });
}

Any simpler alternatives to JSONP, or any other possible solutions I may have not considered would be helpful as well.

解决方案

JSONP simply adds a script tag to the head section and thus is limited only to GET requests. In order to configure your asmx web service to handle JSONP you will need to handle serialization manually:

[WebMethod]
[ScriptMethod(UseHttpGet=true, ResponseFormat=ResponseFormat.Json)]
public string Foo()
{
    var json = new JavaScriptSerializer().Serialize(new 
    {
        Prop1 = "some property",
    });
    string jsoncallback = HttpContext.Current.Request["jsoncallback"];
    return string.Format("{0}({1})", jsoncallback, json);
}

And the client side:

$.getJSON("http://192.9.200.165/ContestWebService/Service1.asmx/PrintOrderReceiptJson?jsoncallback=?",
    function(data) {
        alert(data);
    });

Another alternative for cross domain AJAX calls is to use Flash.

这篇关于发布跨域JSON到ASP.NET使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆