如何使用ajax调用来使用服务 [英] How to consume service using ajax call

查看:176
本文介绍了如何使用ajax调用来使用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







显示 <大>服务电话失败:404没有找到









请帮帮我





问候,

gayathri reddy



我的尝试:









这是我调用wcf休息服务的脚本



$(文件).ready(function() {

$('#btnSubmit')。click(function(event){

debugger

event.preventDefault();

ValidateUser();

});

});



函数ValidateUser( ){

var request ='{objUser:{LoginID:'+ $('#sender-email')。val()+',密码:' + $('#user-pass ).val()+'}}';

// {u:{LoginID:$('#sender-email')。val(),密码:$('#user- pass')。val()}};







var jsondata = JSON.stringify(request );



Type =POST;



Url =http:// localhost:25012 /UserService.svc/AuthenticateUser/;



数据= jsondata,



ContentType =application / JSON; charset = utf-8;



DataType =json;



ProcessData = true; < br $>


//调用Web服务....



CallLoginService();

}





//声明在AJAX方法中使用的Varibales。



var类型;



var Url;



var数据;



var ContentType;



var数据类型;



var ProcessData;



//调用WCF服务的通用函数



函数CallLoginService(){



$ .ajax({



类型:Type,// GET或POST或PUT或DELETE动词



url:Url,//服务的位置



数据:数据,/ /数据发送到服务器



contentType:ContentType,//发送到服务器的内容类型



dataType: DataType,//来自服务器的预期数据格式



processdata:ProcessData,//对或错



crossDomain:true,



成功:函数(msg){



//在成功的服务电话上< br $>


ServiceSucceeded(msg);



},



错误:ServiceFailed //当服务呼叫失败时



});



} b / b
函数ServiceSucceeded(结果){



if(DataType ==json){



resultObject = result.ValidateUserResult;



if(resultObject){



var userdata = JSON.stringify(resultObject);



sessionStorage.userInforamtion = userdata;



window.location =Index.html;



}



else {



alert(无效用户);



$('#sender-email') .val();



$('#user-pass')。val();



}



}



else {



alert(结果数据类型不是JSON);



}



}



功能ServiceFailed(结果){



alert('服务电话失败:'+ result.status +''+ result.statusText);



Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;



}



界面



[ServiceContract]

公共接口IUserService

{

[OperationContract]

[WebInvoke(方法) =POST,ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped,UriTemplate =/ AuthenticateUser)]

用户ValidateUser(用户objUser);



}





class







[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

公共类UserService:IUserService

{

公共用户ValidateUser(用户objUser)

{

用户_obj =新用户();

MembershipUser u = Membership.GetUser(objUser.LoginID);

if(u!= nu ll)

{

if(u.IsApproved)

{

if(Membership.ValidateUser(objUser。 LoginID,objUser.Password))

{

_obj.LoginID = objUser.LoginID;

_obj.UserID = 1;

}

其他

{

_obj.Message =LoginID /密码正确;

}

}

其他

{

_obj.Message =帐户已被停用;

}

}

其他

{

_obj.Message =LoginID不存在;

}

返回_obj;

}

}




it is showing Service call failed: 404Not Found




pls help me


regards,
gayathri reddy

What I have tried:

Hi ,


This is my script to call wcf rest service

$(document).ready(function () {
$('#btnSubmit').click(function (event) {
debugger
event.preventDefault();
ValidateUser();
});
});

function ValidateUser() {
var request = '{"objUser":{"LoginID": "' + $('#sender-email').val() + '", "Password": "' + $('#user-pass').val() + '"}}';
// { u: { LoginID: $('#sender-email').val(), Password: $('#user-pass').val()} };



var jsondata = JSON.stringify(request);

Type = "POST";

Url = "http://localhost:25012/UserService.svc/AuthenticateUser/";

Data = jsondata,

ContentType = "application/json; charset=utf-8";

DataType = "json";

ProcessData = true;

// Call the Web Service....

CallLoginService();
}


// Declare Varibales which are using in AJAX method.

var Type;

var Url;

var Data;

var ContentType;

var DataType;

var ProcessData;

//Generic function to call WCF Service

function CallLoginService() {

$.ajax({

type: Type, //GET or POST or PUT or DELETE verb

url: Url, // Location of the service

data: Data, //Data sent to server

contentType: ContentType, // content type sent to server

dataType: DataType, //Expected data format from server

processdata: ProcessData, //True or False

crossDomain: true,

success: function (msg) {

//On Successfull service call

ServiceSucceeded(msg);

},

error: ServiceFailed // When Service call fails

});

}

function ServiceSucceeded(result) {

if (DataType == "json") {

resultObject = result.ValidateUserResult;

if (resultObject) {

var userdata = JSON.stringify(resultObject);

sessionStorage.userInforamtion = userdata;

window.location = "Index.html";

}

else {

alert("Invalid User");

$('#sender-email').val("");

$('#user-pass').val("");

}

}

else {

alert("Result Data type is not JSON");

}

}

function ServiceFailed(result) {

alert('Service call failed: ' + result.status + '' + result.statusText);

Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;

}

Interface

[ServiceContract]
public interface IUserService
{
[OperationContract]
[WebInvoke(Method="POST", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/AuthenticateUser")]
User ValidateUser(User objUser);

}


class



[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class UserService : IUserService
{
public User ValidateUser(User objUser)
{
User _obj = new User();
MembershipUser u = Membership.GetUser(objUser.LoginID);
if (u != null)
{
if (u.IsApproved)
{
if (Membership.ValidateUser(objUser.LoginID, objUser.Password))
{
_obj.LoginID = objUser.LoginID;
_obj.UserID = 1;
}
else
{
_obj.Message = "LoginID/Password is in correct";
}
}
else
{
_obj.Message = "The Account has been inactivated";
}
}
else
{
_obj.Message = "LoginID does not exists";
}
return _obj;
}
}

推荐答案

(document).ready(function(){
(document).ready(function () {


('#btnSubmit')。click(function(function)事件){

调试器

event.preventDefault();

ValidateUser();

});

});



函数ValidateUser(){

var request ='{objUser:{ LoginID:'+
('#btnSubmit').click(function (event) {
debugger
event.preventDefault();
ValidateUser();
});
});

function ValidateUser() {
var request = '{"objUser":{"LoginID": "' +


('#sender-email')。val()+',密码:'+
('#sender-email').val() + '", "Password": "' +


这篇关于如何使用ajax调用来使用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆