简体   繁体   English

如何从json调用服务器端函数?

[英]How to call server side function from json?

this is my code 这是我的代码

<script type="text/JavaScript">
     var myarray = new array();
     function getsvg1() {

         $.ajax({
         alert("hello");
             type: "post",
             url: "WebForm1.aspx/getsvg1",
             alert("abc");
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function (response) {
                 var cars = response.d;
                 alert(cars);
                 alert("hi");
             },
             failure: function (msg) {

                 $('#output').text(msg);

             }
         });
    }
</SCRIPT>

webservices 网页服务

  [System.Web.Services.WebMethod]
  public static ArrayList getsvg1()
    {

        XDocument doc =        XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/NewFolder1/10000.svg"));
        //XDocument doc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/Orders/100001_PRO/2/svg0.svg"));
        //XNamespace ns1 = "http://www.w3.org/2000/svg";
        //Namespace of a root element can also be retrieved like this:
        //XNamespace ns1 = doc.Root.GetDefaultNamespace();
        //var g = doc.Descendants(ns1 + "image").FirstOrDefault();
        // XDocument doc = XDocument.Load(Server.MapPath("~/excelfiles/svg0.svg"));
        XNamespace ns1 = "http://www.w3.org/2000/svg";
        //Namespace of a root element can also be retrieved like this:
        //XNamespace ns1 = doc.Root.GetDefaultNamespace();

        var retrieveimage = doc.Descendants(ns1 + "image").FirstOrDefault();
        var retrivetext = doc.Descendants(ns1 + "g").FirstOrDefault();
        ArrayList arlelem = new ArrayList();
        foreach (XElement element in doc.Descendants(ns1 + "g"))
        {
            //string[] parts = element.Split(',');
            Console.WriteLine(element);
            arlelem.Add(element);

        }

        // var retrivetext1 = doc.Descendants(ns1 + "text").SelectMany(i => i.ElementExtensions.Select(e => e.GetObject<XElement>().Attribute("url").Value)).ToArray();
        //var retrivetext = doc.Descendants(ns1 + "text").All();
        string v = arlelem[1].ToString();
        string values = retrieveimage.ToString();
        string values1 = retrivetext.ToString();
        char[] delimiterChars1 = { ' ', ',', '"', '\\', '\t', '=' };

        //string text = "one\ttwo three:four,five six seven";
        //System.Console.WriteLine("Original text: '{0}'", text);


        string[] words = values.Split(delimiterChars1);
        string[] words2 = values1.Split(delimiterChars1);
        string[] newword = v.Split(delimiterChars1);
        //Session["newimgwidth"] = words[15];

        return arlelem;
    }

alert is not coming for cars values and breakpoint not going for success and failure. 对于汽车的价值并不会发出警报,对于成功和失败而言,不会出现断点。 in this example im calling server side function from json that function result 在此示例中,我从json调用服务器端函数,该函数结果

To start with your ajax request is filled with syntax errors. 首先,您的ajax请求充满了语法错误。

  1. The $.ajax({ }) block cannot have a alert("hello"); $ .ajax({})块不能有alert("hello"); inside it 在里面
  2. Remove alert("abc"); 删除警报(“ abc”); too
  3. use console.log() instead of alerts in your success method, this is not one of the error but a suggestion/advice. 在成功方法中使用console.log()而不是警报,这不是错误之一,而是建议/建议。
  4. What is your method returning in case of error ? 如果发生错误,您的方法返回什么? In your ajax error method it seems to be expecting a string value. 在您的ajax错误方法中,它似乎期望一个字符串值。
  5. Why are you using type: "post" when you are not posting any data to your method. 当您不向方法中发布任何数据时,为什么要使用type: "post" Use a 'get' instead. 请改用“获取”。
  6. To debug your server side code, try opening the WebForm1.aspx/getsvg1 url in your browser window and see if you get the expected response. 若要调试服务器端代码,请尝试在浏览器窗口中打开WebForm1.aspx/getsvg1 URL,看看是否获得预期的响应。 If all is well next try sending an ajax request using a client like postman rest client to check the response again. 如果一切顺利,接下来尝试使用诸如邮递员休息客户端之类的客户端发送ajax请求,以再次检查响应。

Hope this helps. 希望这可以帮助。

you can use jQuery for this: 您可以为此使用jQuery:

$.getJSON( "http://server.com/webservice", function( data ) {
   console.log(JSON.stringify(data));
}

See more details at: http://api.jquery.com/jquery.getJSON/ 有关更多详细信息,请访问: http : //api.jquery.com/jquery.getJSON/

{key,value } it allow json data.means already availble options or new define json value only. {key,value }它允许json数据。意味着已经可用的选项或仅新定义json值。 you can enter,if you try to alert("hello") it doest allow.so it stopped.so,try without alert message use inside brackets {} . 您可以输入,如果尝试alert("hello")不允许,那么它就停止了。因此,请尝试在括号{}使用警报消息而不使用警报消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM