如何使用javascript将参数传递给WCF服务 [英] How to Pass Parameters to WCF Service using javascript

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

问题描述

大家好,



我正在开发一个HTML应用程序,我只使用JavaScript。这里我可以在不需要传递参数的情况下获取数据,但是传递参数数据时没有绑定,也没有错误抛出。



  function  cbReportWeek(FMonth){
var xmlhttp;

// var reporturl =http:// localhost:12045 / Service.svc / ReportWeekDropDown?FiscalMonth =+ FMonth;

if window .XMLHttpRequest){ // IE7 +,Firefox,Chrome,Opera,Safari的代码
xmlhttp = new XMLHttpRequest();
}
其他 { // IE6,IE5的代码
xmlhttp = ActiveXObject( Microsoft.XMLHTTP);
}
// alert(FMonth);
xmlhttp。 onreadystatechange = function (){
debugger
if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ){

var Items = JSON .parse(xmlhttp。 responseText的);
var data = Items;
var 选项;
var length = Items.ToFillReportWeekDropDownResult.length;
for var i = 0 ; i< length; i ++){
$( #cbxReportWeek) .append( < option> + Items.ToFillReportWeekDropDownResult [i] .ReportWeekName + < / option>);
}

document .getElementById(' cbxReportWeek')。selectedIndex = 1 ;
}
else {
document .getElementById(' cbxReportWeek')。innerHTML = ' < option>选择Week< / option>';
}
}
xmlhttp.open( POST http:// localhost:12045 / Service.svc / ReportWeekDropDown);
xmlHttp.setRequestHeader( Content-type application / json);
xmlhttp.send( FiscalMonth = {' + FMonth + '});
// xmlhttp.send();
alert(xmlhttp.responseXML) ;
alert(xmlhttp.responseText);
}





有没有告诉我我在哪里做错了。非常感谢任何帮助。



谢谢,

D Haraveer。

解决方案

#cbxReportWeek)。append( < option> + Items.ToFillReportWeekDropDownResult [i] .ReportWeekName + < /选项>中);
}

document .getElementById(' cbxReportWeek')。selectedIndex = 1 ;
}
else {
document .getElementById(' cbxReportWeek')。innerHTML = ' < option>选择Week< / option>';
}
}
xmlhttp.open( POST http:// localhost:12045 / Service.svc / ReportWeekDropDown);
xmlHttp.setRequestHeader( Content-type application / json);
xmlhttp.send( FiscalMonth = {' + FMonth + '});
// xmlhttp.send();
alert(xmlhttp.responseXML) ;
alert(xmlhttp.responseText);
}





有没有告诉我我在哪里做错了。非常感谢任何帮助。



谢谢,

D Haraveer。


Hello Dinesh,



你可以使用jquery如下:

  //  创建Person对象,区分大小写 
var personObj = {name: Imdad,phonenumber: 91< /跨度>};

// 发布成功后的回调函数
< span class =code-keyword> var
onAddUserSuccess = function (returnData){
/ / 如果需要,使用returnData执行某些操作
};
// 发布到您的网络服务或网页


< blockquote> .ajax({
type: POST
url: Users.aspx / AddPerson
data:personObj,
contentType: application / json; charset = utf-8
dataType: json
成功:onAddUserSuccess
});


Hi All,

I'm developing an HTML application which I'm using only JavaScript. Here i'm able to get data when there is no need of passing parameters but while passing parameters data is not binding and also no error is throwing.

function cbReportWeek(FMonth) {
            var xmlhttp;

            //var reporturl = "http://localhost:12045/Service.svc/ReportWeekDropDown?FiscalMonth=" + FMonth;

            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            //alert(FMonth);
            xmlhttp.onreadystatechange = function () {
                debugger
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                    var Items = JSON.parse(xmlhttp.responseText);
                    var data = Items;
                    var options;
                    var length = Items.ToFillReportWeekDropDownResult.length;
                    for (var i = 0; i < length; i++) {
                        $("#cbxReportWeek").append("<option>" + Items.ToFillReportWeekDropDownResult[i].ReportWeekName + "</option>");
                    }

                    document.getElementById('cbxReportWeek').selectedIndex = 1;
                }
                else {
                    document.getElementById('cbxReportWeek').innerHTML = '<option>Select Week</option>';
                }      
            }
            xmlhttp.open("POST", "http://localhost:12045/Service.svc/ReportWeekDropDown", true);
            xmlHttp.setRequestHeader("Content-type", "application/json");
            xmlhttp.send("FiscalMonth={'" + FMonth+"'}");
            //xmlhttp.send();
            alert(xmlhttp.responseXML);
            alert(xmlhttp.responseText);
        }



does any tell me where i'm doing wrong.Any help is much appreciated.

Thanks,
D Haraveer.

解决方案

("#cbxReportWeek").append("<option>" + Items.ToFillReportWeekDropDownResult[i].ReportWeekName + "</option>"); } document.getElementById('cbxReportWeek').selectedIndex = 1; } else { document.getElementById('cbxReportWeek').innerHTML = '<option>Select Week</option>'; } } xmlhttp.open("POST", "http://localhost:12045/Service.svc/ReportWeekDropDown", true); xmlHttp.setRequestHeader("Content-type", "application/json"); xmlhttp.send("FiscalMonth={'" + FMonth+"'}"); //xmlhttp.send(); alert(xmlhttp.responseXML); alert(xmlhttp.responseText); }



does any tell me where i'm doing wrong.Any help is much appreciated.

Thanks,
D Haraveer.


Hello Dinesh,

You can use jquery as follow:

// create the Person object, case sensitive
var personObj = {name: "Imdad", phonenumber: "91"};

// the callback function when post is successful
var onAddUserSuccess= function(returnData) { 
  // do something with the returnData if needed 
};
// the post to your webservice or page


.ajax({ type: "POST", url: "Users.aspx/AddPerson", data: personObj, contentType: "application/json; charset=utf-8", dataType: "json", success: onAddUserSuccess });


这篇关于如何使用javascript将参数传递给WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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