Posting cross-domain JSON to ASP.NET with jQuery

前端 未结 1 452
抹茶落季
抹茶落季 2020-12-17 05:35

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

相关标签:
1条回答
  • 2020-12-17 06:03

    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.

    0 讨论(0)
提交回复
热议问题