ASP .NET AJAX和JQuery

I'm trying to set up a simple JQuery example in order to make AJAX calls to a .NET webservice. Using the following example below I'm getting AJAX errors that are just saying 0 in the result instead of any meaningful message:

Javascript Call

function QSHelloWorld() {
    var options = {
        type: "POST",
        url: "http://localhost:1087/QueryService.asmx/HelloWorld",
        data: "{}",
        contentType: "application/json",
        dataType: "json",
        success: AjaxSucceeded,  
        error: AjaxFailed
    };

    $.ajax(options);    
}

function AjaxSucceeded(result) {  
    alert(result.d);  
}  

function AjaxFailed(result) {  
    alert("Error: " + result.status +  " " + result.statusText);  
}

ASP .NET WebSite

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jQueryTest._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="javascript" type="text/javascript" src="js/jquery-1.3.2-vsdoc2.js" />
<script language="javascript" type="text/javascript" src="js/qsAJAX.js" />

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="formMain" runat="server">
    <div>
        <script type="text/javascript">
            QSHelloWorld();
        </script> 
    </div>
    </form>
</body>
</html>

ASP .NET WebService

using System.Web.Script.Services;
using System.Web.Services;

namespace QueryService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class QueryService : WebService
    {
        [WebMethod]
        [ScriptMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

When I make a call to the QSHelloWorld I get a messagebox saying Error: 0 with no further information.

I'm currently running this example using Windows 7, do I need to have anything specifically installed besides the .NET Framework 3.5 SP1 in order to get this to run properly?

Thanks,

Daven

3个回答


问题是javascript文件正在Chrome中加载文件,而不是在IE中加载文件。 在ASP.NET Default.aspx文件中进行以下更改之后,一切似乎都可以正常工作。</ p>

更改:</ p>

  <脚本语言=“ javascript” type =“ text / javascript” src =“ js / jquery-1.3.2-vsdoc2.js” />

</ code> </ pre>

到</ p>

   &lt;/ script&gt;<br>
 <script type =“ text / javascript” language =“ javascript” src =“ / js / qsAJAX.js” /> &lt;/ script&gt;<br>
 &lt;/ code&gt; &lt;/ pre&gt;<br>
     &lt;/ div&gt;</p>

展开原文

原文

The issue is that the javascript files were loading file in Chrome, but not in IE. After making the following change in the ASP .NET Default.aspx file, everything seemed to work.

Changing:

<script language="javascript" type="text/javascript" src="js/jquery-1.3.2-vsdoc2.js" />
<script language="javascript" type="text/javascript" src="js/qsAJAX.js" />

to

<script type="text/javascript" language="javascript" src="/js/jquery-1.3.2-vsdoc2.js"></script>
<script type="text/javascript" language="javascript" src="/js/qsAJAX.js"/></script>

展开原文

原文

Maybe this page will help you out. Their example uses JSON as well.


尝试直接在.ASPX页中加载JQuery时遇到问题。 相反,我有一个ProjectBasePage类,在它的PageLoad中做到了这一点:</ p>

  Page.ClientScript.RegisterClientScriptInclude(typeof(ProjectBasePage),
            “ jQuery”,ResolveUrl(“〜/ js / jquery-1.3.2.min.js”));
</ code> </ pre>

它对我有用... </ p>
     </ div>

展开原文

原文

I've had problems when trying to load JQuery directly in the .ASPX page. Instead I have a ProjectBasePage class that in it's PageLoad does this:

   Page.ClientScript.RegisterClientScriptInclude(typeof(ProjectBasePage),
           "jQuery", ResolveUrl("~/js/jquery-1.3.2.min.js"));

It works for me...

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐