gpt4 book ai didi

jquery - 使用 jQuery ajax 中的参数调用 WebMethod 失败

转载 作者:行者123 更新时间:2023-12-01 02:05:34 24 4
gpt4 key购买 nike

我对以下问题感到困惑。

我有一个“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 的版本吗?
  • 我应该使用“Fiddler”吗? (还没用过,学习曲线)
  • IE11 中的 F12 开发者工具有帮助吗?
  • IIS Express 是否在我的开发 PC 上本地运行?

任何帮助将不胜感激。

我的浏览器有什么问题吗?在谷歌搜索页面上出现了很多上面有波浪号的“A”,我在开发者机器上从 IE 提交这个问题时遇到了问题。在我的普通电脑上提交没问题。某种编码问题?

最佳答案

更改ajax调用中的“data”参数,例如数据:JSON.stringify({ Msg: 'Hello Client'}),并定义您的网络方法,例如:-[WebMethod]公共(public)静态字符串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;

}

关于jquery - 使用 jQuery ajax 中的参数调用 WebMethod 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23076984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com