JQuery:调用Web服务

时间:2010-08-25 11:06:39

标签: javascript jquery silverlight asmx

我正在开发一个SilverLight应用程序,其中在浏览器关闭事件中,我需要进行Web服务调用。我有一个Web服务方法,它接受一个参数。当用户点击浏览器关闭事件时。我将调用doRelease()函数。 releaseuser方法需要一个参数usertoken。

当我调用jQuery函数CallService()时出错。

  

行:186   错误:预期的对象

 var varType;
    var varUrl;
    var varData;
    var varContentType;
    var varDataType;
    var varProcessData;
    //Generic function to call AXMX/WCF  Service        
    function CallService() {
        $.ajax({
            type: varType, //GET or POST or PUT or DELETE verb
            url: varUrl, // Location of the service
            data: varData, //Data sent to server
            contentType: varContentType, // content type sent to server
            dataType: varDataType, //Expected data format from server
            processdata: varProcessData, //True or False
            success: function (msg) {//On Successfull service call
                alert("success");
                ServiceSucceeded(msg);
            },
            error: ServiceFailed// When Service call fails
        });
    }

    function Temp(usertoken) {
        varType = "POST";
        varUrl = "http://localhost/TempWS/MachineHistoryWS.asmx?op=ReleaseUser";
        varData = usertoken;
        varContentType = "application/json; charset=utf-8";
        varDataType = "json";
        varProcessData = true;

        alert("call service");

        CallService();

    }
    function ServiceSucceeded(result) {//When service call is sucessful

        alert("success");

        varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null;
    }
    function ServiceFailed(result) {
        alert('Service call failed: ' + result.status + '' + result.statusText);
        varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null;
    }



    function doRelease() {

        var usertoken = readCookie("usertoken");


        Temp("usertoken");
    }

3 个答案:

答案 0 :(得分:1)

我解决了我的问题,但没有使用jquery。这是我的解决方案。

function sendDataAsXML_SOAP() {
        var req_params = "", url = "", number = 0, type = "";
        /* Configure Parameters */
        url = "http://localhost/TempWS/MachineHistoryWS.asmx";
        user = "129272802615082804";

        req_params = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
        req_params = req_params + "<soap:Body><ReleaseUser>";
        req_params = req_params + "<credentials>" + user + "</credentials></ReleaseUser></soap:Body></soap:Envelope>";
        alert(req_params);
        /* Send XML/SOAP Request To Web Service Using Browser's Javascript DOM */
        try {
            ajax_request = new XMLHttpRequest();
        }
        catch (trymicrosoft) {
            try {
                ajax_request = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (othermicrosoft) {
                try {
                    ajax_request = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (failed) {
                    ajax_request = false;
                }
            }
        }
        ajax_request.open("POST", url, true);
        ajax_request.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
        ajax_request.onreadystatechange = receiveXML_SOAPData;
        ajax_request.send(req_params);
    }

    function receiveXML_SOAPData() {
        if (ajax_request.readyState == 4) {
            if (ajax_request.status == 200) {
                alert(ajax_request.responseText);

            }
        }
    }

答案 1 :(得分:0)

这看起来有点奇怪: -

function doRelease() { 

    var usertoken = readCookie("usertoken"); 


    Temp("usertoken"); 
} 

首先我们假设readCookie正在做正确的事情?

其次,最后一行应该是: -

    Temp(usertoken);

第三,所有这些中的“Silverlight”角度在哪里?

答案 2 :(得分:0)

必须将数据包装为对象。在CallService函数中,更改:

data: varData, 

到:

data: "{input:'" + varData + "'}",

将“输入”更改为Web服务方法中的实际参数名称。