Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii,

I have made a WCF REST service which returns the response in JSON format and now I want to call it from javascript.But when I try to call it I couldn't view any response.
To make a javascript call I have written this:
XML
<script src="Scripts/jquery-1.5.2.min.js" type="text/javascript">
   </script>
   <script language="javascript" type="text/javascript">
       function Greeting() {
          
           $("#btnWCFREST").click(function() {
           $.ajax({
           url:"http://localhost:8732/Services/RoleService/json/Role/provider",
          
           type:"GET",
           dataType:"json",
         
            contentType:"application/json; charset=utf-8",
            successs:function(msg){
            alert(msg);
            },
            Error:function(msg){
            alert("Failed");
            }
            }

            );
            }
            }


   </script>


How can I get the response.Where Am I going wrong?

Thanks..
Posted
Comments
hitesh_tech 29-Aug-12 11:44am    
What are you getting in alert(msg); ?
cutie1 29-Aug-12 12:35pm    
I even cant view the alert (msg)..I cant view any action on page load when I click the button

1 solution

<script type="text/javascript">

var Type;
var Url ="http://..... "// service URL;
var Data;
var ContentType;
var DataType;
var ProcessData;
var method;
//Generic function to call WCF Service


function CallService() {
$.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
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}

function ServiceFailed(result) {
Type = null;
Url = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}


function GetEmployee() {
var uesrid = "1";
Type = "POST";
ContentType = "application/json; charset=utf-8";
Data = "{'Location=Pune'}";
DataType = "jsonp"; ProcessData = false;
method = "sayHello";
CallService();
}

function ServiceSucceeded(result) {
if (DataType == "jsonp") {
if (method == "CreateEmployee") {
alert(result);
}
else {

alert("Succ 1 - " + result)
var string = result[1].Actionname + " \n " + result[0].Actionname;
alert(string);
}
}
}
}

$(document).ready(
function () {
try {
GetEmployee();
} catch (exception) { }
}
);



</script>
 
Share this answer
 
v2

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