Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从外部服务器从C WebService调用JSON方法_C#_Javascript_Json_Web Services_Cross Domain - Fatal编程技术网

C# 如何从外部服务器从C WebService调用JSON方法

C# 如何从外部服务器从C WebService调用JSON方法,c#,javascript,json,web-services,cross-domain,C#,Javascript,Json,Web Services,Cross Domain,如何在HTML5/JavaScript站点中使用c中的asp.net web服务? 而且web服务与客户端不在同一个站点,因此这将是一个跨域请求。我在web编程方面有一点经验,当我将所有asp.net页面移动到HTML5和JS以获得更快的界面时,我在尝试将web服务连接到JS时遇到了真正的困难,JSON就是答案,接收到的信息从2Kb下降到128字节,因此在移动设备上它在某种程度上是有用的 第一/服务器端 mywebservice.asmx 下一个web服务将托管在站点site.com中 然后地址

如何在HTML5/JavaScript站点中使用c中的asp.net web服务?
而且web服务与客户端不在同一个站点,因此这将是一个跨域请求。

我在web编程方面有一点经验,当我将所有asp.net页面移动到HTML5和JS以获得更快的界面时,我在尝试将web服务连接到JS时遇到了真正的困难,JSON就是答案,接收到的信息从2Kb下降到128字节,因此在移动设备上它在某种程度上是有用的

第一/服务器端

mywebservice.asmx

下一个web服务将托管在站点site.com中

然后地址将是Site.com/MYWS.asmx/JSONMethod 希望这对你有用,就像对我有用一样。 祝你好运和快乐


-Poncho

这不是一个问题不,不是,但这是一个可以解决如何使用的问题的答案,我查看了StackOverflow常见问题解答,他们鼓励发布带有答案的问题,因此如果这至少可以帮助一个人,我的工作就完成了。我理解你想做什么,但这不是一个问题和答案。这是对一个问题和答案的最简短的描述。
[WebService(Namespace = "http://...")]
[System.Web.Script.Services.ScriptService]
public class MYWS
{
    [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public string JSONMethod(/*ParType ParName, ParType ParName2,.. etc*/)
        {
                List<object> tempobjects = new List<object>();
                tempobjects.Add(new { ID = id, Val = val /*more params=> ParName= "ParValue", etc..*/ });

                var retVal = new JavaScriptSerializer().Serialize(tempobjects.ToArray());

                Context.Response.ContentType = "application/json";
                var p = "";
                if (Context.Request.Params.AllKeys.Contains("callback") == true)
                    p = Context.Request.Params["callback"].ToString();
        Context.Response.Write(string.Format(CultureInfo.InvariantCulture, "{0}({1})", p, retVal));
        }
}
function MyJSMethod(){
$.ajax({
    url: "Site.com/MYWS.asmx/JSONMethod",
    data:{ParName: "parValue"/*, ParName2... etc*/},
    type:"GET",
    dataType:"jsonp" //if the method
    })
    .done(
        function(data){
            //Do what you need with the json data retrieved
            //... CODE HERE ...

            //In case you need to call a method from another js being used
            //window["JSmethod"](data)//if the json received is needed
        }
    ).fail(function(jqXHR, textStatus, errorThrown ){
    alert(jqXHR);
    alert(textStatus);
    alert(errorThrown);
    });
}