C# 从 jQuery 调用 ASMX
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/879362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling ASMX from jQuery
提问by
I am trying to call an ASMX method from jQuery without success. Following is my code, and I don't understand what I am missing.
我试图从 jQuery 调用 ASMX 方法但没有成功。以下是我的代码,我不明白我错过了什么。
File Something.js,
文件Something.js,
function setQuestion() {
$.ajax({
type: "POST",
data: "{}",
dataType: "json",
url: "http: //localhost/BoATransformation/Survey.asmx/GetSurvey",
contentType: "application/json; charset=utf-8",
success: onSuccess
});
}
function onSuccess(msg) {
$("#questionCxt").append(msg);
}
File SomethingElse.cs,
文件SomethingElse.cs,
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {
public Survey () {
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string GetSurvey() {
return "Question: Who is Snoopy?";
}
}
回答by Jim Scott
One thing that stands out is you have UseHttpGet=true
but in your jQuery code you are using POST.
突出的一件事是您拥有UseHttpGet=true
但在您的 jQuery 代码中使用 POST。
Also here is a test page I created calling an ASMX page.
这里还有一个我创建的测试页面,它调用了一个 ASMX 页面。
[WebMethod]
public Catalog[] GetCatalog()
{
Catalog[] catalog = new Catalog[1];
Catalog cat = new Catalog();
cat.Author = "Jim";
cat.BookName ="His Book";
catalog.SetValue(cat, 0);
return catalog;
}
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "default.asmx/GetCatalog",
cache: false,
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: handleHtml,
error: ajaxFailed
});
});
function handleHtml(data, status) {
for (var count in data.d) {
alert(data.d[count].Author);
alert(data.d[count].BookName);
}
}
function ajaxFailed(xmlRequest) {
alert(xmlRequest.status + ' \n\r ' +
xmlRequest.statusText + '\n\r' +
xmlRequest.responseText);
}
</script>
回答by CSharpAtl
Here is an example of a jQuery call to a page method on an aspx, but it would be similar to an asmx page.
这是一个 jQuery 调用 aspx 页面方法的示例,但它类似于 asmx 页面。
$.ajax(
{
type: "POST",
url: "NDQA.aspx/ValidateRoleName",
data: '{"roleName":"' + $('[id$=RoleNameTextBox]').val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: ValidateSuccess,
error: ValidateError
});
回答by Rick Hochstetler
I would also suggest removing UseHttpGet as Jim Scott suggested.
我还建议按照 Jim Scott 的建议删除 UseHttpGet。
You can add the following to your options and check the objXMLHttpRequest to see a more detailed error response.
您可以将以下内容添加到您的选项并检查 objXMLHttpRequest 以查看更详细的错误响应。
error: function(objXMLHttpRequest, textStatus, errorThrown) {
debugger;
}
回答by Josef Pfleger
You have to make sure you specify Json as the response format if that is what you want and get rid of UseHttpGet
due to security features:
您必须确保将 Json 指定为响应格式,如果这是您想要的并且UseHttpGet
由于安全功能而摆脱:
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string GetSurvey() {
return "Question: Who is Snoopy?";
}
回答by Sean Smith
You have to make sure you specify Json as the response format if that is what you want and get rid of UseHttpGet due to security features:
您必须确保将 Json 指定为响应格式,如果这是您想要的,并且由于安全功能而摆脱 UseHttpGet:
If you read that article then you would see that it is safe to use UseHttpGet as ASP.NET has features to block the cross site scripting attack vector.
如果您阅读那篇文章,那么您会发现使用 UseHttpGet 是安全的,因为 ASP.NET 具有阻止跨站点脚本攻击向量的功能。
There are plenty of valid reasons to use GET.
使用 GET 有很多正当理由。
He can remove the data parameter and change POST to GET to make the call work. Assuming you want a JSON response it would be required to add ResponseFormat=ResponseFormat.Json as well.
他可以删除 data 参数并将 POST 更改为 GET 以使调用工作。假设您需要 JSON 响应,还需要添加 ResponseFormat=ResponseFormat.Json。
回答by user609926
I came across this question and had the same issue. I solved it by adding:
我遇到了这个问题并遇到了同样的问题。我通过添加解决了它:
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
Below your web method attribute, if you'd like to use POST. ie:
在您的网络方法属性下方,如果您想使用 POST。IE:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {
public Survey () {
}
[WebMethod]
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
[ScriptMethod(UseHttpGet = true)]
public string GetSurvey() {
return "Question: Who is Snoopy?";
}
}
回答by Taha Karaca
If you try chrome browser, try internet explorer it worked for me and also it is about chrome browser you must add extension to works in chrome but i dont know the name of extension
如果您尝试使用 chrome 浏览器,请尝试使用 Internet Explorer,它对我有用,而且它与 chrome 浏览器有关,您必须在 chrome 中添加扩展程序,但我不知道扩展程序的名称
回答by balaji palamadai
The following Steps solved my problem, hope it helps some one,
以下步骤解决了我的问题,希望对某人有所帮助,
To allow this Web Service to be called from script, using ASP.NET AJAX, include the following line above your asmx service class for example
[System.Web.Script.Services.ScriptService] public class GetData : System.Web.Services.WebService {
Add protocols under system.web in web.config, please click on the link if you are not able to view configuration
要允许使用 ASP.NET AJAX 从脚本调用此 Web 服务,请在 asmx 服务类上方包含以下行,例如
[System.Web.Script.Services.ScriptService] 公共类 GetData : System.Web.Services.WebService {
在web.config中system.web下添加协议,如果看不到配置请点击链接
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>