我对以下问题感到困惑。
我有'WebForm1.aspx'和'WebService1.asmx'。 当我在WebService WITHOUT参数('HelloWorld')中调用WebMethod时,它工作正常。当我调用Method WITH参数('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);}
});
});
我正在使用的代码来自互联网上的示例。我尝试将参数传递为表单编码(conmmented out)和JSON。似乎没什么用。
怎么办?
非常感谢任何帮助。
我的浏览器有什么问题吗?在我的开发者机器上,有很多'上面有一个带有波形符号的A出现在googled页面上,我在IE中提交这个问题时遇到了问题。在我的普通PC上,提交很好。有些编码问题吗?
答案 0 :(得分:9)
在ajax调用中更改&#34; data&#34; 参数 数据:JSON.stringify({Msg:&#39; Hello Client&#39;}), 并定义您的网络方法,如: - 的 [的WebMethod] 强> public static string GetArray(string Msg) 的 {强>
返回消息;
<强>} 强>
在ajax调用中传入数据参数时,必须相同方法参数名称。
您可以在下面看到以下示例: -
$(document).ready(function() {
debugger;
$.ajax({
type: "POST",
url: "flight-result-online.aspx/GetArray",
data: JSON.stringify({ title: 'MP3', songname: 'Gulabi Ankhe' }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
},
error: function(msg) {
alert(msg.d);
}
});
});
并且您的Web方法定义如下: -
[WebMethod]
public static string GetArray(string title, string songname)
{
return title+" "+songname;
}