从asp.net中的js文件调用webmethod [英] calling webmethod from js file in asp.net

查看:83
本文介绍了从asp.net中的js文件调用webmethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有一个jscript文件,例如:

hi all
i have one jscript file like:

saveSticky = function saveSticky() {
	    $.ajax({
	        type: "POST",
	        url: "Home.aspx/MethodSample",
	        data: "{}",
	        contentType: "application/json; charset=utf-8",
	        dataType: "json",
	        success: function succ(msg) {
	            alert('Called Ajax Method');
	        },
	        failure: function (response) {
	            alert(response);
	        }
	    });
	  
	    var that = $(this), sticky = (that.hasClass("sticky-status") || that.hasClass("sticky-content")) ? that.parents('div.sticky') : that,
                obj = {
                    id: sticky.attr("id"),
                    top: sticky.css("top"),
                    left: sticky.css("left"),
                    text: write = sticky.children(".sticky-content").html().search(document.getElementById("lblusername").value) <= 0 ? sticky.children(".sticky-content").html() + document.getElementById("lblusername").value : sticky.children(".sticky-content").html()
                }
	    localStorage.setItem("sticky-" + obj.id, JSON.stringify(obj));

	    sticky.find(".sticky-status").text("saved");
	    // alert('end to save stickey sticky-' + obj.id + ' ' + JSON.stringify(obj));
	}


是调试脚本代码,它工作正常,我在运行代码时未收到任何错误,但
jquery方法不会每次都调用webmethod
我的网络方法就像:


am debug script code it works fine i did''t get any errors while running code but
jquery method wont calling webmethod every time
my web method is like:

[WebMethod]
   public static void MethodSample()
   {
       string str = "Hello World";
   }


这样我就尝试调用像这样的方法:


so that i tried calling method like:

url: "organozation/Home.aspx/MethodSample"


但再次它无法正常工作,我尝试使用其他类型的分配,但没有得到结果,所以我如何在这里调用Web方法...
任何人都可以帮我吗..
在此先感谢


but again it wont working i tried allot with different type but am not getting result so how can i call web method here...
can any one please help me..
thanks in advance

推荐答案

.ajax({ 类型:" , url:" , 数据:" , contentType:" , dataType:" , 成功:功能 succ(msg){ alert(' 被称为Ajax方法'); }, 失败:功能(响应){ 警报(响应); } }); var that =
.ajax({ type: "POST", url: "Home.aspx/MethodSample", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function succ(msg) { alert('Called Ajax Method'); }, failure: function (response) { alert(response); } }); var that =


( this ),粘性=(that .hasClass(" )|| that.hasClass( 粘性内容"))? that.parents(' div.sticky'): obj = { id:sticky.attr(" ), 顶部:sticky.css(" ), 左:sticky.css(" ), 文字:write = sticky.children(" ).html().search( 文档 .getElementById(" ) .value)< = 0 吗? sticky.children(" ).html()+ " lblusername").value:sticky.children (" ).html() } localStorage.setItem(" + obj.id, JSON .stringify(obj)); sticky.find(" ).text( 已保存"); // alert('结束保存Stickey Sticky-'+ obj.id +''+ JSON.stringify(obj) ); }
(this), sticky = (that.hasClass("sticky-status") || that.hasClass("sticky-content")) ? that.parents('div.sticky') : that, obj = { id: sticky.attr("id"), top: sticky.css("top"), left: sticky.css("left"), text: write = sticky.children(".sticky-content").html().search(document.getElementById("lblusername").value) <= 0 ? sticky.children(".sticky-content").html() + document.getElementById("lblusername").value : sticky.children(".sticky-content").html() } localStorage.setItem("sticky-" + obj.id, JSON.stringify(obj)); sticky.find(".sticky-status").text("saved"); // alert('end to save stickey sticky-' + obj.id + ' ' + JSON.stringify(obj)); }


是调试脚本代码,它工作正常,我在运行代码时未收到任何错误,但
jquery方法不会每次都调用webmethod
我的网络方法就像:


am debug script code it works fine i did''t get any errors while running code but
jquery method wont calling webmethod every time
my web method is like:

[WebMethod]
   public static void MethodSample()
   {
       string str = "Hello World";
   }


这样我就尝试调用像这样的方法:


so that i tried calling method like:

url: "organozation/Home.aspx/MethodSample"


但再次它无法正常工作,我尝试使用其他类型的分配,但没有得到结果,所以我如何在这里调用Web方法...
任何人都可以帮我吗..
预先感谢


but again it wont working i tried allot with different type but am not getting result so how can i call web method here...
can any one please help me..
thanks in advance


您需要在web.config中添加此代码,以启用从javascript调用WebMethods

You need to add this code in web.config to enable calling WebMethods from javascript

<system.web>
  <httpmodules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </httpmodules>
</system.web>






我错过了ScriptHandler的确切版本

对于VS2008.您需要使用此




Edited:

I missed the exact version for the ScriptHandler

For VS 2008. You need to use this

<httpModules>
       <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
     </httpModules>


这篇关于从asp.net中的js文件调用webmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆