admin管理员组文章数量:1415664
I am calling this from an iFrame! I think this would be the main problem.
I am trying to call a WebMethod in ASP with an ajax request. But I am not getting the desired respone. This is the code for my page. And I call it like
Ext.Ajax.request({url: 'Default.aspx/DispatchMethod'});
The problem is I get html in the response instead of the string "TEST". Any idea why?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static String DispatchMethod()
{
return "TEST";
}
}
After using a WebService (.asmx extension) I just get a
This web service is using / as its default namespace.
Remendation: Change the default namespace before the XML Web service is made public.
Each XML Web service needs a unique namespace in order for client applications to distinguish it from other services on the Web. / is available for XML Web services that are under development, but published XML Web services should use a more permanent namespace.
Your XML Web service should be identified by a namespace that you control. For example, you can use your pany's Internet domain name as part of the namespace. Although many XML Web service namespaces look like URLs, they need not point to actual resources on the Web. (XML Web service namespaces are URIs.)
For XML Web services creating using ASP.NET, the default namespace can be changed using the WebService attribute's Namespace property. The WebService attribute is an attribute applied to the class that contains the XML Web service methods. Below is a code example that sets the namespace to "/":
C#
I am calling this from an iFrame! I think this would be the main problem.
I am trying to call a WebMethod in ASP with an ajax request. But I am not getting the desired respone. This is the code for my page. And I call it like
Ext.Ajax.request({url: 'Default.aspx/DispatchMethod'});
The problem is I get html in the response instead of the string "TEST". Any idea why?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static String DispatchMethod()
{
return "TEST";
}
}
After using a WebService (.asmx extension) I just get a
This web service is using http://tempuri/ as its default namespace.
Remendation: Change the default namespace before the XML Web service is made public.
Each XML Web service needs a unique namespace in order for client applications to distinguish it from other services on the Web. http://tempuri/ is available for XML Web services that are under development, but published XML Web services should use a more permanent namespace.
Your XML Web service should be identified by a namespace that you control. For example, you can use your pany's Internet domain name as part of the namespace. Although many XML Web service namespaces look like URLs, they need not point to actual resources on the Web. (XML Web service namespaces are URIs.)
For XML Web services creating using ASP.NET, the default namespace can be changed using the WebService attribute's Namespace property. The WebService attribute is an attribute applied to the class that contains the XML Web service methods. Below is a code example that sets the namespace to "http://microsoft./webservices/":
C#
Share Improve this question edited May 12, 2015 at 20:55 luca.p.alexandru asked May 12, 2015 at 18:28 luca.p.alexandruluca.p.alexandru 1,7606 gold badges24 silver badges46 bronze badges 1- Set the Content-Type to the appropriate MIME. See here: stackoverflow./questions/17185814/… – Howard Renollet Commented May 12, 2015 at 18:55
3 Answers
Reset to default 1I'm not sure if your code structure was intended for the purpose of the Q, If not, then you may need start by decoupling and focus on a key element of "ContentType" in your AJAX request.
Start by adding a .asmx (which is what [WebMethod] refers too) file to your solution.
Then add the method:
[WebMethod]
public string HelloWorld()
{
return "Hello, World";
}
Now assuming your using an external js sheet, apply the following logic (Onload or OnClick):
$.ajax
({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/webservice-folder/webservice.asmx/HelloWorld",
success: (function (data) {
alert(data.d)
}),
error: (function () {
alert("error");
})
});
Okay, let's clear something up. You're using a page method, not an XML Web Service, and you really really don't want to go down the dark path of using an asmx service just to expose an endpoint for client script.
Your page method syntax looks all right. Your issue is probably with the options you've provided (most likely because the content-type is not being set as application/json).
You mentioned you get HTML back. Well, what kind of HTML? Could it perhaps be an error page?
I think you need the url to be: "DispatchMethod"
Could be wrong.
You also might need more fields, like this:
$.ajax({
dataType: "json",
url: "DispatchMethod",
data: {
"customerNo": customerNo,
"productNo": productNo
},
本文标签: cASP NET call Webmethod with ajax request from javascriptStack Overflow
版权声明:本文标题:c# - ASP .NET call Webmethod with ajax request from javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745201552a2647401.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论