您能告诉我此代码中的问题所在:
$('#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文件位于同一文件夹中。 我尝试了很多,但没有找到解决方案的运气。非常感谢任何帮助!
答案 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>