通过jQuery调用ASMX失败 [英] Call ASMX via jQuery fails
问题描述
VS2010
ASP.Net 4.0
解决方案:带有1个项目的Test.sln
Web应用程序
项目:测试
Test.aspx(客户代码)
Test.aspx.cs(网络方法)
这样有用:
VS2010
ASP.Net 4.0
Solution: Test.sln with 1 project
Web Application
Project: Test
Test.aspx (client code)
Test.aspx.cs (web method)
This works:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BlendSpecAlerts.aspx.cs" Inherits="ADS.BlendOptimizer.BlendSpecAlerts" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/json.js"></script>
<script type="text/javascript">
function GetData() {
var data = new Object();
data.Message= "1";
$.ajax({
url: "Test.aspx/GetData",
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(data),
success: function (result) {
alert(result.d)
}
})
}
</script>
<input type="button" önclick="Getdata()"/>
</body>
</html>
Test.aspx.cs
Test.aspx.cs
[WebMethod()]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetData(string message)
{
return "I returned your " + message
这不起作用:
现在,当我创建一个新项目时使用Test.ASMX服务在Test.sln MyWebService中进行ct并包含如下所示的相同Web方法:
在jquery调用中:
This does NOT work:
Now, when I create a new project within Test.sln MyWebService with Test.ASMX service and include the same web method like below:
In the jquery call:
url: "http://localhost/MyWebService/Test.aspx/GetData"
[WebService(Namespace = "http://blabla")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Test: System.Web.Services.WebService
{
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string GetData(string message)
{
return "I returned your " + message
}
结果:
Result:
Error in: http://localhost/Test/MyWebService/Test.asmx/Test
error: undefined
type: ajaxError
readystate: 4
status: 500
我可以通过粘贴http:// localhost / test / test直接访问.asmx .asmx /测试
我得到了一个结果。
很长时间以来我一直在寻找解决方案,(跨领域问题)我按照建议设置Web服务以允许跨域请求)等web.config设置..)
我在这里结束了我的智慧。我觉得我需要旁边有人告诉我我做错了什么。
请不要提供一些有神奇解决方案的链接,我一直在那里。
为什么这不起作用!?
谢谢,
Robert
I can access the .asmx directly ok by pasting in http://localhost/test/test.asmx/Test
and I get a result.
For many hours I have looked for solutions, (cross domain issues (to which I follow the recommendations to setup the web service to allow a cross domain request), etc, web.config settings.. )
I am at my wits end here. I feel I need someone next to me to show me what am I doing wrong.
Please don't provide some link that will have the magical solution, I have been there.
Why is this not working!?
Thanks,
Robert
推荐答案
.ajax({
url: Test.aspx / GetData,
类型: POST,
contentType : application / json; charset = utf-8,
dataType: json,
data: JSON .stringify(数据),
成功:功能(结果){
alert(result.d)
}
})
}
< / script >
< 输入 type = 按钮 önclick = Getdata() / >
< / body >
< / html >
.ajax({ url: "Test.aspx/GetData", type: "POST", contentType: "application/json;charset=utf-8", dataType: "json", data: JSON.stringify(data), success: function (result) { alert(result.d) } }) } </script> <input type="button" önclick="Getdata()"/> </body> </html>
Test.aspx .cs
Test.aspx.cs
[WebMethod()]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetData(string message)
{
return "I returned your " + message
这不起作用:
现在,当我创建Test.sln MyWebService中的一个新项目,带有Test.ASMX服务,并包含与下面相同的Web方法:
在jquery调用中:
This does NOT work:
Now, when I create a new project within Test.sln MyWebService with Test.ASMX service and include the same web method like below:
In the jquery call:
url: "http://localhost/MyWebService/Test.aspx/GetData"
[WebService(Namespace = "http://blabla")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Test: System.Web.Services.WebService
{
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string GetData(string message)
{
return "I returned your " + message
}
结果:
Result:
Error in: http://localhost/Test/MyWebService/Test.asmx/Test
error: undefined
type: ajaxError
readystate: 4
status: 500
我可以通过粘贴http://localhost/test/test.asmx/Test
直接访问.asmx,我得到一个结果。
我花了很多时间寻找解决方案,(跨域问题(我按照建议设置Web服务以允许跨域请求)等,web .config设置..)
我在这里的智慧结束了。我觉得我需要旁边有人告诉我我做错了什么。
请不要提供一些有神奇解决方案的链接,我一直在那里。
为什么这不起作用!?
谢谢,
Robert
I can access the .asmx directly ok by pasting in http://localhost/test/test.asmx/Test
and I get a result.
For many hours I have looked for solutions, (cross domain issues (to which I follow the recommendations to setup the web service to allow a cross domain request), etc, web.config settings.. )
I am at my wits end here. I feel I need someone next to me to show me what am I doing wrong.
Please don't provide some link that will have the magical solution, I have been there.
Why is this not working!?
Thanks,
Robert
亲爱的罗伯特,
请注意,如果您的参数是字符串格式,那么请使用单引号,例如
Ex。数据:{'message':'1'},
和
如果您的参数是整数/双格式,那么不要使用任何东西。
Ex。数据:{'消息':1},
试试这个:
Dear Robert,
Please note, if your parameter is in string format then use Single Quote like
Ex. data: "{'message':'1'}",
and
In case your parameter is in interger/double format then do not use anything.
Ex. data: "{'message':1}",
Try this:
function GetData() {
var msg="1";
.ajax({
url: Test.aspx / GetData,
类型: POST,
contentType: application / json; charset = utf-8,
dataType: json,
data: {'message':' + msg + '},
成功: function (结果){
al ert(result.d)
}
});
}
.ajax({ url: "Test.aspx/GetData", type: "POST", contentType: "application/json;charset=utf-8", dataType: "json", data: "{'message':'"+msg+"'}", success: function (result) { alert(result.d) } }); }
这篇关于通过jQuery调用ASMX失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!