Ok, so I created a test project just to verify that jQuery AJAX works with asp.net service, and it does no problems. I used a default HelloWorld service created in VS studio. I am calling the service via jQuery like this:
好的,我创建了一个测试项目来验证jQuery AJAX与asp.net服务是否兼容,并且没有问题。我使用了在VS studio中创建的默认HelloWorld服务。我通过jQuery这样调用服务:
in Default.aspx:
在default . aspx:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
//test web service
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "TestService.asmx/HelloWorld",
data: "{}",
dataType: "json",
success: function(msg) { alert(msg);},
error: function (XMLHttpRequest, textStatus, errorThrown) {
debugger;
}
});
});
</script>
in TestService.asmx
在TestService.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebServiceTestWitJQuery
{
/// <summary>
/// Summary description for TestService
/// </summary>
[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 TestService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
I then proceeded and copied everything exactly as it is in my project and it does not work. I get a 500 server error.
然后我继续并复制了所有的东西,就像在我的项目中一样,它不起作用。我得到一个500服务器错误。
I verified following:
我核实后:
- web.configs identical
- 网络。配置相同
- pages identical
- 页面相同
- service class identical
- 服务类的
- jquery ajax call identical
- jquery ajax调用相同的
- I can navigate to http://localhost:3272/TestService.asmx?op=HelloWorld and webservice works fine.
- 我可以导航到http://localhost:3272/TestService.asmx?HelloWorld和webservice运行良好。
What else?
还有什么?
6 个解决方案
#1
2
Figured it out. When not sure what is happening use Fiddler. It clearly shows that server could not create an instance of the service class because it was in the wrong namespace. I had R# disabled and did not notice that I did not change the namespace of the service. Problem solved.
算出来。当不确定发生了什么时,使用Fiddler。它清楚地显示服务器无法创建服务类的实例,因为它位于错误的名称空间中。我禁用了r#,并且没有注意到我没有更改服务的名称空间。问题解决了。
#2
1
I have a couple of guesses as I know a bit about jQuery but not anything about asp.net.
我对jQuery有一些了解,但对asp.net一无所知,所以我有一些猜测。
On the jQuery call, you are indicating you want a json response. "Hello World" I would think will be returned as a raw string, not formatted as json. Remove the 'dataType: "json",' param and jQuery should do it's best to determine the received datatype.
在jQuery调用中,表示需要json响应。“Hello World”将作为原始字符串返回,而不是格式化为json。删除“数据类型”:“json”、“param”和“jQuery”应该尽可能地确定接收的数据类型。
I think the way you have written your jQuery code is only compatible with RESTful Web Services, not conventional web services with a WSDL. Although I don't know anything about asp.net, I don't see anything in the naming conventions here you are using REST.
我认为您编写jQuery代码的方式只与RESTful Web服务兼容,而不是使用WSDL的传统Web服务。虽然我对asp.net一无所知,但是在命名约定中我没有看到任何使用REST的东西。
#3
0
Problem is in your Script URL so you need to Write full URL. Here is your solution. EG: URL:localhost:1111/TestService.asmx/HelloWorld
问题是在您的脚本URL中,所以您需要编写完整的URL。这是您的解决方案。例如:URL:localhost:1111 / TestService.asmx / HelloWorld
#4
0
i solved this. i know too late for answer this question but may be somebody has same problem.
我解决了这个问题。我知道回答这个问题太晚了,但是可能有人有同样的问题。
i removed below lines from ajax and 500 internal error disabled.
我从ajax中删除了以下代码行,并禁用了500个内部错误。
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
#5
0
I was receiving this error and it was caused by my WebService not having access to the session state. This is fixed by changing
我收到了这个错误,它是由于我的WebService无法访问会话状态引起的。这是通过改变来解决的
[WebMethod]
public MyData GetMyData() {...}
to
来
[WebMethod(EnableSession = true)]
public MyData GetMyData() {...}
See this link for more information : How to use Session state in ASP.NET WebService
有关更多信息,请参见此链接:如何在ASP中使用会话状态。网网络服务
#6
0
I post this, because neither of the top solutions worked for me (VS2015, .Net4.0, ajax call in WebForms exactly like in the question). It worked only after I explicitly authorized the HttpPost in the config file like this :
我发布了这篇文章,因为这两种*解决方案对我都不起作用(VS2015、. net4.0、ajax在WebForms中的调用与问题中完全一样)。只有在我在配置文件中明确授权HttpPost之后,它才会工作:
<system.web>
<webServices>
<protocols>
<add name="HttpGet" /> -- just for direct testing : delete this line after
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>
#1
2
Figured it out. When not sure what is happening use Fiddler. It clearly shows that server could not create an instance of the service class because it was in the wrong namespace. I had R# disabled and did not notice that I did not change the namespace of the service. Problem solved.
算出来。当不确定发生了什么时,使用Fiddler。它清楚地显示服务器无法创建服务类的实例,因为它位于错误的名称空间中。我禁用了r#,并且没有注意到我没有更改服务的名称空间。问题解决了。
#2
1
I have a couple of guesses as I know a bit about jQuery but not anything about asp.net.
我对jQuery有一些了解,但对asp.net一无所知,所以我有一些猜测。
On the jQuery call, you are indicating you want a json response. "Hello World" I would think will be returned as a raw string, not formatted as json. Remove the 'dataType: "json",' param and jQuery should do it's best to determine the received datatype.
在jQuery调用中,表示需要json响应。“Hello World”将作为原始字符串返回,而不是格式化为json。删除“数据类型”:“json”、“param”和“jQuery”应该尽可能地确定接收的数据类型。
I think the way you have written your jQuery code is only compatible with RESTful Web Services, not conventional web services with a WSDL. Although I don't know anything about asp.net, I don't see anything in the naming conventions here you are using REST.
我认为您编写jQuery代码的方式只与RESTful Web服务兼容,而不是使用WSDL的传统Web服务。虽然我对asp.net一无所知,但是在命名约定中我没有看到任何使用REST的东西。
#3
0
Problem is in your Script URL so you need to Write full URL. Here is your solution. EG: URL:localhost:1111/TestService.asmx/HelloWorld
问题是在您的脚本URL中,所以您需要编写完整的URL。这是您的解决方案。例如:URL:localhost:1111 / TestService.asmx / HelloWorld
#4
0
i solved this. i know too late for answer this question but may be somebody has same problem.
我解决了这个问题。我知道回答这个问题太晚了,但是可能有人有同样的问题。
i removed below lines from ajax and 500 internal error disabled.
我从ajax中删除了以下代码行,并禁用了500个内部错误。
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
#5
0
I was receiving this error and it was caused by my WebService not having access to the session state. This is fixed by changing
我收到了这个错误,它是由于我的WebService无法访问会话状态引起的。这是通过改变来解决的
[WebMethod]
public MyData GetMyData() {...}
to
来
[WebMethod(EnableSession = true)]
public MyData GetMyData() {...}
See this link for more information : How to use Session state in ASP.NET WebService
有关更多信息,请参见此链接:如何在ASP中使用会话状态。网网络服务
#6
0
I post this, because neither of the top solutions worked for me (VS2015, .Net4.0, ajax call in WebForms exactly like in the question). It worked only after I explicitly authorized the HttpPost in the config file like this :
我发布了这篇文章,因为这两种*解决方案对我都不起作用(VS2015、. net4.0、ajax在WebForms中的调用与问题中完全一样)。只有在我在配置文件中明确授权HttpPost之后,它才会工作:
<system.web>
<webServices>
<protocols>
<add name="HttpGet" /> -- just for direct testing : delete this line after
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>