[英]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请求充满了语法错误。
alert("hello");
alert("hello");
inside it type: "post"
when you are not posting any data to your method. type: "post"
。 Use a 'get' instead. 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. 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.