【发布时间】:2014-05-29 09:50:26
【问题描述】:
我对以下问题感到困惑。
我有一个“WebForm1.aspx”和一个“WebService1.asmx”。 当我在没有参数('HelloWorld')的 WebService 中调用 WebMethod 时,它工作正常。当我调用带有参数的方法('SayHello')时,它失败了。
它甚至没有命中方法(我在方法中设置的断点没有到达)。 xmlHttpRequest 错误是“内部服务器错误”
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string SayHello(string firstName, string lastName)
{
return "Hello " + firstName + " " + lastName;
}
}
我的 WebForms1.aspx:
<div><br />No Parameters </div>
<div id="NoParameters"></div>
<div><br />With Parameters</div>
<div id="WithParameters"></div>
<script type="text/javascript">
$(document).ready(function () {
// SayHello returns a string we want to display. Examples A, B and C show how you get the data in native
// format (xml wrapped) as well as in JSON format. Also how to send the parameters in form-encoded format,
// JSON format and also JSON objects. To get JSON back you need to send the params in JSON format.
// Test - call a function that returns a string.
// No Parameters
$.ajax({
type: "POST",
url: "WebService1.asmx/HelloWorld",
data: "{}",
dataType: "text",
success: function (data) {
$("#NoParameters").html(data); // show the string that was returned, this will be the data inside the xml wrapper
}
});
// Example A - call a function that returns a string.
// Params are sent as form-encoded, data that comes back is text
$.ajax({
type: "POST",
url: "WebService1.asmx/SayHello",
//data: "firstName=Aidy&lastName=F", // the data in form-encoded format, ie as it would appear on a querystring
//contentType: "application/x-www-form-urlencoded; charset=UTF-8", // if you are using form encoding, this is default so you don't need to supply it
data: "{firstName:'Aidy', lastName:'F'}", // the data in JSON format. Note it is *not* a JSON object, is is a literal string in JSON format
contentType: "application/json; charset=utf-8", // we are sending in JSON format so we need to specify this
dataType: "text", // the data type we want back, so text. The data will come wrapped in xml
success: function (data) {
$("#WithParameters").html(data); // show the string that was returned, this will be the data inside the xml wrapper
}
, error: function(xmlHttpRequest, status, err) {alert(err);}
});
});
我使用的代码来自互联网上的示例。我尝试以表单编码(已注释掉)和 JSON 的形式传递参数。似乎没有任何效果。
怎么办?
- 会不会是jQuery 1.8.2的版本?
- 我应该使用“提琴手”吗? (还没用过,学习曲线)
- IE11 中的 F12 开发人员工具会有所帮助吗?
- IIS Express 是否在我的开发 PC 上本地运行?
任何帮助将不胜感激。
我的浏览器有什么问题吗?谷歌搜索页面上显示了很多上面带有波浪号的“A”,我在开发人员机器上从 IE 提交这个问题时遇到了问题。在我的普通电脑上提交很好。某种编码问题?
【问题讨论】:
-
我在另一台 PC 上尝试了整个项目。相同的操作系统,相同的 VS2013 相同的 IE11 并且它可以工作!。
标签: jquery ajax webforms webmethod