使用 JqueryMobile 调用 Web 服务不起作用

发布于 2024-10-16 22:58:16 字数 1857 浏览 2 评论 0原文

你能告诉我这段代码中的问题出在哪里吗:

$('#callAjax').live('click', function (event) {
    // $("#resultLog").html("Working....");

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Hello.asmx/HelloWorld",
        data: "{}",
        dataType: "json",
        success: function (msg) {
            alert(msg);
            //$("#resultLog").html(msg);
        }
    }); // end of ajax


    $("#resultLog").ajaxError(function (event, request, settings, exception) {

        $("#resultLog").html("Error Calling: " + settings.url + "<br />HTPP Code: " + request.status);

    });


});  // end of click

HTML 文件中的一些代码

<input id="callAjax" type="button" value="Call Ajax" />

     <div id="resultLog"></div>

这是我的网络服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // 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 WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public static string HelloWorld() {
        return "Hello World";
    }

}

返回值是: Error Calling: Hello.asmx/HelloWorld HTTP 代码:500

ASMX 文件与我在其中调用 Web 服务的自定义 JavaScript 文件位于同一文件夹中。 我尝试了很多,但没有找到解决方案。非常感谢对此的任何帮助!

Can you tell me where the problem is in this code:

$('#callAjax').live('click', function (event) {
    // $("#resultLog").html("Working....");

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Hello.asmx/HelloWorld",
        data: "{}",
        dataType: "json",
        success: function (msg) {
            alert(msg);
            //$("#resultLog").html(msg);
        }
    }); // end of ajax


    $("#resultLog").ajaxError(function (event, request, settings, exception) {

        $("#resultLog").html("Error Calling: " + settings.url + "<br />HTPP Code: " + request.status);

    });


});  // end of click

some code in in the HTML file

<input id="callAjax" type="button" value="Call Ajax" />

     <div id="resultLog"></div>

Here is my web-service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;

    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // 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 WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public static string HelloWorld() {
        return "Hello World";
    }

}

The return value is: Error Calling: Hello.asmx/HelloWorld
HTPP Code: 500
.

ASMX file is in same folder as my custom JavaScript file in which I call the web-service.
I tried a lot, but had no luck in finding the solution. Greatly appreciate any help on this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2024-10-23 22:58:16

尝试设置 $.mobile.allowCrossDomainPages = true; 并检查 $.support.cors 是否设置为 true 。如果不是,则明确将其设置为 true

<script type="text/javascript">
alert("changed" + " " + $.mobile.allowCrossDomainPages);
$(window.document).bind("mobileinit", function() {
    alert("mobileinit" + " " + $.support.cors);
    $.mobile.allowCrossDomainPages = true;
});
</script>

Try setting $.mobile.allowCrossDomainPages = true; and also check if $.support.cors is set to true or not. If not then set it to true explicitly:

<script type="text/javascript">
alert("changed" + " " + $.mobile.allowCrossDomainPages);
$(window.document).bind("mobileinit", function() {
    alert("mobileinit" + " " + $.support.cors);
    $.mobile.allowCrossDomainPages = true;
});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文