jQuery的AJAX与ASP.NET WebMethod的返回整页 [英] Jquery AJAX with ASP.NET WebMethod Returning Entire Page

查看:111
本文介绍了jQuery的AJAX与ASP.NET WebMethod的返回整页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一些简单的测试(以preparation一个较大的项目)使用JQuery AJAX调用一个ASP.NET的WebMethod。在我的例子,我的WebMethod返回一个简单的字符串。然而,当我尝试使用jQuery来调用它,我得到的返回而不是只是我的字符串的整个HTML页面内容。我在想什么?

客户端:

  $(文件)。就绪(函数准备(){
        $(#用作MyButton)。点击(函数点击(E){
            $。员额(Default.aspx的/ TestMethod的,
                {名字:鲍勃},
                函数(MSG){
                    警报(数据:收到:+味精);
                },
                HTML
            );
        });
    });
 

服务器端:

 使用系统;
使用System.Web.Services;

命名空间JqueryAjaxText
{
    公共部分类_Default:System.Web.UI.Page
    {
        保护无效的Page_Load(对象发件人,EventArgs的)
        {

        }

        [WebMethod的]
        公共静态字符串TestMethod的(字符串名称)
        {
            返回提交的值是+名称;
        }
    }
}
 

解决方案

看看这个链接。我用他的一些其他职位,以calll WCF服务的成功。一定要检查出的相关文章:

<一个href="http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/">http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

通过文章阅读,但它的本质:

  $(文件)。就绪(函数(){
  $(#结果)。点击(函数(){
    $阿贾克斯({
      键入:POST,
      网址:Default.aspx的/ GETDATE
      数据: {},
      的contentType:应用/ JSON的;字符集= UTF-8,
      数据类型:JSON,
      成功:函数(MSG){
        $(#结果)文本(msg.d)。
      }
    });
 

I'm doing some simple tests (in preparation for a larger project) to call an ASP.NET WebMethod using JQuery AJAX. In my example, my WebMethod returns a simple string. However, when I attempt to call it using JQuery, I get the entire HTML page content returned instead of just my string. What am I missing?

Client Side :

$(document).ready(function ready() {
        $("#MyButton").click(function clicked(e) {
            $.post("Default.aspx/TestMethod",
                {name:"Bob"},
                function(msg) {
                    alert("Data Recieved: " + msg);
                },
                "html"
            );
        });
    });

Server Side:

using System;
using System.Web.Services;

namespace JqueryAjaxText
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static string TestMethod(string name)
        {
            return "The value submitted was " + name;
        }
    }
}

解决方案

Check out this link. I used some of his other posts to calll WCF service with success. Be sure to check out the related articles:

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Read through the article but its essentially:

$(document).ready(function() {
  $("#Result").click(function() {
    $.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        $("#Result").text(msg.d);
      }
    });

这篇关于jQuery的AJAX与ASP.NET WebMethod的返回整页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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