使用JQuery从WebMethod获取数据 [英] Get Data from WebMethod Using JQuery
问题描述
在后面的代码中(在SOEDefault.aspx.cs中),有一个像这样的WebMethod
In the code behind (in SOEDefault.aspx.cs), there is a WebMethod like that
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string GetData() {
string routeInput = "A1";
string measureInput = "100";
string url = "http://.../Segment_Operation?LRSSegInput="
+ routeInput + "&MeasureInputA=" + measureInput + "&MeasureInputB=" + "1000&f=pjson";
var webClient = new System.Net.WebClient();
string d = webClient.DownloadString(url);
return d;
}
在.aspx
In the .aspx
$.ajax( {
type:'Get',
url: 'SOEDefault.aspx/GetData',
dataType: 'text',
data: '{}',
crossDomain : true,
success:function(results) {
console.log('Success!');
debugger;
ajaxData = results;
},
error: function(xhr, status, error) {
debugger;
console.log(error);
}
});
我一步一步调试。获得的ajaxData是SOEDefault.aspx的全文,而不是后面代码中GetData()方法的数据。网址有什么问题?谢谢。
I debugged step-by-step. The ajaxData obtained is the full text of the SOEDefault.aspx, rather than the data from the GetData() method in the code behind. What's wrong in the url? Thanks.
推荐答案
.ajax({
type:' 获取',
url:' SOEDefault.aspx / GetData',
dataType:' text',
data:' {}',
crossDomain: true ,
成功: function (结果){
console .log(< span class =code-string>' 成功!');
调试器;
ajaxData = results;
},
error: function (xhr,status,error){
调试器;
控制台跨度> .log中(误差);
}
});
.ajax( { type:'Get', url: 'SOEDefault.aspx/GetData', dataType: 'text', data: '{}', crossDomain : true, success:function(results) { console.log('Success!'); debugger; ajaxData = results; }, error: function(xhr, status, error) { debugger; console.log(error); } });
我一步一步调试。获得的ajaxData是SOEDefault.aspx的全文,而不是后面代码中GetData()方法的数据。网址有什么问题?谢谢。
I debugged step-by-step. The ajaxData obtained is the full text of the SOEDefault.aspx, rather than the data from the GetData() method in the code behind. What's wrong in the url? Thanks.
访问这里......
http://stackoverflow.com/questions/17655807/how-to-send-and-retrieve- data-from-web-method-using-jquery-ajax-call [ ^ ]
或
http://stackoverflow.com/questions/8517071/send-json-data-via-post-ajax-and- receive-json-response-from-controller-mvc [ ^ ]
visit here...
http://stackoverflow.com/questions/17655807/how-to-send-and-retrieve-data-from-web-method-using-jquery-ajax-call[^]
or
http://stackoverflow.com/questions/8517071/send-json-data-via-post-ajax-and-receive-json-response-from-controller-mvc[^]
WebMethod只能返回xml或json格式 - 即使你用jQuery编写其他格式...
所以你需要在你的ajax调用中添加一行
WebMethod can only return xml or json format - even if you write in jQuery other formats...
So you need to add one line to your ajax call
contentType: "application/json; charset=utf-8"
或
or
contentType: "application/xml; charset=utf-8",
根据您配置WEbMethod的类型(默认为json)...
一个样本
default.aspx.cs
according to what type you configured your WEbMethod (default is json)...
A sample
default.aspx.cs
[WebMethod]
[ScriptMethod( UseHttpGet = true )]
public static string GetData ( )
{
return ( "Hello!" );
}
< br $> b $ b
default.aspx
default.aspx
这篇关于使用JQuery从WebMethod获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!