使用Jquery Mobile调用Web服务将无法正常工作

时间:2011-02-09 17:34:42

标签: jquery webservices-client

您能告诉我此代码中的问题所在:

$('#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 HTPP Code: 500

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

1 个答案:

答案 0 :(得分:0)

尝试设置$.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>