jQuery的:到asp.net web服务的Ajax调用失败 [英] jQuery: Ajax call to asp.net webservice fails
问题描述
这是Web服务
好了,我创建了一个项目只是为了验证jQuery的AJAX可与asp.net的服务,它也没有任何问题。我用注册,我在工作室VS创建方法。我打电话通过jQuery服务是这样的:
this is web service Ok, so I created a project just to verify that jQuery AJAX works with asp.net service, and it does no problems. I used Registering method that i created in VS studio. I am calling the service via jQuery like this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Try
{
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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
{
[WebMethod]
public string Registering(string user3gmail, string pass3, string name3, string nickname3,string birth)
{
return "{\"registerPachage\":{\"Id\":26}}";
}
}
}
这是jQuery脚本
/// <reference path="jquery-1.12.0.min.js" />
$(document).ready(function () {
$("#signUp").on("click", function () {
Register();
});
function Register() {
$.ajax({
url: "http://localhost:1052/WebService.asmx/Registering/" + $("#emailId").val() + "/" + $("#Password").val() + "/" + $("#Username").val() + "/" + $("#nickName").val() + "/1994-02-28",
success: function (data) {
alert("Done");
window.location.href = "index.html";
},
error: function (data) {
alert("Error");
}
});
}
});
为什么不走的index.html?和打印错误
why doesn’t go to "index.html"? and print error
推荐答案
你加
<asp:scriptmanager ID="Scriptmanager1" runat="server">
<Services>
<asp:ServiceReference runat="server" Path="~/path/to/webservice"/>
</Services>
</asp:scriptmanager>
在您的asp.net网站form.If不添加它。您还需要通过服务引用添加到项目中的右键点击引用 - >添加服务引用,并添加您的服务引用
in your asp.net web form.If not add it. You also need to add service reference to your project by Right click on reference -> Add service references and add your service reference
您可以调用此方法双向
1.JAVASCRIPT
$(document).ready(function () {
YourNameSpace.WebService.Registering("user3gmail","pass3","name3","nickname3","birth",onSuccess);
function onSuccess(result) {
alert(result);
}
});
2.JQuery
$(document).ready(function () {
$("#signUp").on("click", function (e) {
e.preventDefault();
Register();
});
function Register() {
var params = {};
params.user3gmail= "test";
params.pass3= "pass3";
//rest goes here.All data must be present
$.ajax({
type: "POST",
url:"http://localhost:1052/WebService.asmx/Registering",
crossDomain: true,
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert("SUCCESS="+data.d);
},
error: function(data) {
alert("error=" + data.d);
}
});
}
});
这篇关于jQuery的:到asp.net web服务的Ajax调用失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!