Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all i got the error, while calling web-method in HTML page using jquery.

<soap:envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:body><soap:fault><soap:code><soap:value>soap:Receiver<soap:reason><soap:text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.

at System.Xml.XmlTextReaderImpl.Throw(Exception e)

at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)

at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()

at System.Xml.XmlTextReaderImpl.ParseDocumentContent()

at System.Xml.XmlTextReaderImpl.Read()

at System.Xml.XmlTextReader.Read()

at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()

at System.Xml.XmlReader.MoveToContent()

at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()

at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()

at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()

at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)

at System.Web.Services.Protocols.SoapServerProtocol.Initialize()

at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)


my script like this
$(document).ready(function () {


$("#btnok").click(function () {

// var json = Sys.Serialization.JavaScriptSerializer.serialize(obj);
var title = $("#title").val();
var name = $("#name").val();
var city = $("#city").val();
var state = $("#state").val();
var date = $("#date").val();
var description = $("#description").val();
var qualification = $("#qualification").val();
var Compensation ="";
var openings = "";
var phone = "";
var email = "";
var zipcode = "";
var job_categories ="";
var client_id = "";
var data = { title: $("#title").val(), name: $("#name").val(), city: $("#city").val(), state: $("#state").val(), date: $("#date").val(), Compensation:"", openings:"", phone:"", email:"", zipcode:"", description: $("#description").val(), qualification: $("#qualification").val(), job_categories:"", client_id:"2" };


// var data = { title:$("#title").val(),name: $("#name").val(), city: $("#city").val(), state: $("#state").val(), date: $("#date").val(),duration:$("#duration").val(),pay:$("#pay").val(),type: $("#type").val(),industry:$("#industry").val(),status:$("#status").val(),description:$("#description").val(),qualification: $("#qualification").val() };
var data = JSON.stringify(data);
alert(data);
$.ajax({
type: "POST",
url: "http://localhost:3658/HostingService.asmx/SETPostJobDetails",
data:JSON.stringify(data),//"{'title':" + title + ",'C.Name':" + name + ",'city':" + city + ",'state':" + state + ",'date':" + date + ",'duration':" + duratin + ",'pay':" + pay + ",'industry':" + industry + ",'type':" + type + ",'status':" + status + ",'description':" + description + ",'qualification':" + qualification + "}",
// data: // "{'title':'" + $("#title").val() + "','C.Name':'" + $("#name").val() + "','city':'" + $("#city").val() + "','state':'" + $("#state").val() + "','date':'" + $("#date").val() + "','duration':'" + $("#duration").val() + "','pay':'" + $("#pay").val() + "','industry':'" + $("#industry").val() + "','type':'" + $("#type").val() + "','status':'" + $("#status").val() + "','description':'" + $("#description").val() + "','qualification':'" + $("#qualification").val() + "'}",
contentType: "application/json;charset=utf-8",
processData: false,
dataType: "json",
success: function (data) {
alert('Success');
},
error: function (data, status, error) {
alert(data.responseText);

}
});
});
});
please help me
Posted

1 solution


Common error:

  • make sure that you have referenced jquery
  • make sure your parameter name on ajax call matches the parameter name defined on the webservice method
  • make sure you add ScriptService attribute on your webservice, [System.Web.Script.Services.ScriptService]
  • make sure you add webmethod attribute on your public webservice methods, [WebMethod]
  • ...the list continues, anyone please mention your common error so we learn form each other. Thanks


Here is a complete sample



Html


<html>
    <head>
    <!--Make sure you update  this reference -->
        <script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    </head>
        <body>
            <input type="text" id="userName" />
            <input type="password" id="password" />
            <input type="button" id="login" value="Submit" />
        </body>

</html>



Javascript


    $("document").ready(
    function () {
        $("#login").click(
            function () {

                //get the data from input controls
                var uName = $("#userName").val();
                var pass = $("#password").val();

                //construct your key-value pair
                var data = { userName: uName, password: pass }

                //create the object notation (JSON)
                var jsonData = JSON.stringify(data);

                //make POST to the hander service
                $.ajax({
                    type: "POST",
                    url: "/Services/HandlerWebService.asmx/LogInUser",
                    data: jsonData,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccessHandler,
                    error: OnErrorHandler
                });


                //handler if the callback is successful
                function OnSuccessHandler(data) {
                    alert(data.d);
                }

                //handler if the callback raise error
                function OnErrorHandler(xhr, status, error) {
                    var err = eval("(" + xhr.responseText + ")");
                    alert(err.Message);
                }
            }
        );
    }
);



Webservice called HandlerWebService.asmx in Services folder


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace jQueryWebService.jQueryPostRequest
{
    /// <summary>
    /// Summary description for HandlerWebService
    /// </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 HandlerWebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string LogInUser(string userName, string password)
        {
            //Check user credentials on server/database and login user here
            return string.Format("Welcome {0}, you are logged in.", userName);
        }
    }
}


I hope this helps, cheers.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900