Calling web service using javascript error

Posted by politrok on Stack Overflow See other posts from Stack Overflow or by politrok
Published on 2010-02-02T14:52:35Z Indexed on 2010/05/14 18:04 UTC
Read the original article Hit count: 472

Hi , I try to call webservice using json , when i call to web service without excepted parameters , its work , but when i'm try to send parameter , ive got an error :

This is my code:

function GetSynchronousJSONResponse(url, postData) 
{

    var xmlhttp = null;
    if (window.XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    else if (window.ActiveXObject) {
        if (new ActiveXObject("Microsoft.XMLHTTP"))
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        else
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }

    url = url + "?rnd=" + Math.random(); // to be ensure non-cached version

    xmlhttp.open("POST", url, false);
    xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    xmlhttp.send(postData);
    var responseText = xmlhttp.responseText;
    return responseText;
}


function Test() 
{
    var result = GetSynchronousJSONResponse('http://localhost:1517/Mysite/Myservice.asmx/myProc', '{"MyParam":"' + 'test' + '"}');
    result = eval('(' + result + ')');
    alert(result.d);
}

This is the error :

System.InvalidOperationException: Request format is invalid: application/json; charset=utf-8.

at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()

at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest

What is wrong ?

Thanks in advance .

© Stack Overflow or respective owner

Related posts about webservice-client

Related posts about JavaScript