How to call server side function from json?

时间:2022-01-04 15:00:09

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 i m calling server side function from json that function result

汽车价值和断点不是成功和失败的警报。在这个例子中,我从json调用服务器端函数,即函数结果

3 个解决方案

#1


0  

To start with your ajax request is filled with syntax errors.

从ajax请求开始会出现语法错误。

  1. The $.ajax({ }) block cannot have a alert("hello"); inside it
  2. $ .ajax({})块不能有警报(“hello”);在里面

  3. Remove alert("abc"); too
  4. 删除警报(“abc”);太

  5. use console.log() instead of alerts in your success method, this is not one of the error but a suggestion/advice.
  6. 使用console.log()代替成功方法中的警报,这不是错误之一,而是建议/建议。

  7. What is your method returning in case of error ? In your ajax error method it seems to be expecting a string value.
  8. 如果出错,您的方法返回了什么?在你的ajax错误方法中,似乎期待一个字符串值。

  9. Why are you using type: "post" when you are not posting any data to your method. Use a 'get' instead.
  10. 当你没有向你的方法发布任何数据时,为什么使用类型:“post”。请改用“获取”。

  11. 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. If all is well next try sending an ajax request using a client like postman rest client to check the response again.
  12. 要调试服务器端代码,请尝试在浏览器窗口中打开WebForm1.aspx / getsvg1 url,看看是否得到了预期的响应。如果一切顺利,请尝试使用postman rest client之类的客户端发送ajax请求,以再次检查响应。

Hope this helps.

希望这可以帮助。

#2


0  

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/

#3


0  

{key,value } it allow json data.means already availble options or new define json value only. you can enter,if you try to alert("hello") it doest allow.so it stopped.so,try without alert message use inside brackets {}.

{key,value}它允许json data.means只有可用的选项或新的定义json值。你可以输入,如果你试图警告(“你好”)它doest allow.so它停止。所以,尝试没有警报消息使用括号{}。

#1


0  

To start with your ajax request is filled with syntax errors.

从ajax请求开始会出现语法错误。

  1. The $.ajax({ }) block cannot have a alert("hello"); inside it
  2. $ .ajax({})块不能有警报(“hello”);在里面

  3. Remove alert("abc"); too
  4. 删除警报(“abc”);太

  5. use console.log() instead of alerts in your success method, this is not one of the error but a suggestion/advice.
  6. 使用console.log()代替成功方法中的警报,这不是错误之一,而是建议/建议。

  7. What is your method returning in case of error ? In your ajax error method it seems to be expecting a string value.
  8. 如果出错,您的方法返回了什么?在你的ajax错误方法中,似乎期待一个字符串值。

  9. Why are you using type: "post" when you are not posting any data to your method. Use a 'get' instead.
  10. 当你没有向你的方法发布任何数据时,为什么使用类型:“post”。请改用“获取”。

  11. 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. If all is well next try sending an ajax request using a client like postman rest client to check the response again.
  12. 要调试服务器端代码,请尝试在浏览器窗口中打开WebForm1.aspx / getsvg1 url,看看是否得到了预期的响应。如果一切顺利,请尝试使用postman rest client之类的客户端发送ajax请求,以再次检查响应。

Hope this helps.

希望这可以帮助。

#2


0  

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/

#3


0  

{key,value } it allow json data.means already availble options or new define json value only. you can enter,if you try to alert("hello") it doest allow.so it stopped.so,try without alert message use inside brackets {}.

{key,value}它允许json data.means只有可用的选项或新的定义json值。你可以输入,如果你试图警告(“你好”)它doest allow.so它停止。所以,尝试没有警报消息使用括号{}。