使用 jQuery 将跨域 JSON 发布到 ASP.NET

发布于 2024-08-17 06:45:36 字数 900 浏览 3 评论 0原文

遇到了一个棘手的问题。

我正在开发一个项目,当用户在我们的网站上的信息亭结帐时,我们需要允许打印收据。由于与驱动程序和格式相关的原因,我使用 COM 自动化和 Word 来处理收据的打印。我已将此代码封装在本地计算机上运行的 Web 服务中。

计划是在页面 html 中对运行 Web 服务的本地计算机的 url 进行简单的 jQuery ajax 调用。该ajax调用包含订单的json对象,该对象由Web服务反序列化并打印出来。如果我使用 localhost,则工作正常,但是在生产中,我将违反无跨域 ajax 调用规则。

代理将不起作用,因为网站上运行的代码无法联系运行打印服务的本地 Web 服务。在网上浏览后,我发现使用 JSONP 可能是解决此问题的方法,但我不知道如何使其工作。大多数教程假设您正在尝试获取一些远程数据,而不仅仅是简单地发布数据。打印 Web 服务返回无效。

如何配置我的 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); }
    });
}

任何 JSONP 的更简单的替代方案,或者我可能没有考虑过的任何其他可能的解决方案也会有所帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2024-08-24 06:45:37

JSONP 只是将 script 标记添加到 head 部分,因此仅限于 GET 请求。为了配置您的 asmx Web 服务来处理 JSONP,您需要手动处理序列化:

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

客户端:

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

跨域AJAX调用就是使用Flash

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文