使用 JqueryMobile 调用 Web 服务不起作用
你能告诉我这段代码中的问题出在哪里吗:
$('#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试设置
$.mobile.allowCrossDomainPages = true;
并检查$.support.cors
是否设置为true
。如果不是,则明确将其设置为true
:Try setting
$.mobile.allowCrossDomainPages = true;
and also check if$.support.cors
is set totrue
or not. If not then set it totrue
explicitly: