Click here to Skip to main content
15,035,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I want to pass JSON Object to WCF service,
Here is my service method:
C#
[OperationContract]
       [WebInvoke(Method="POST", RequestFormat=WebMessageFormat.Json,     ResponseFormat=WebMessageFormat.Json,        UriTemplate = "/UplodImageAndroid")]
       bool uploadImageAndroid(CustomImage custImg);


C#
public bool uploadImageAndroid(CustomImage custImg)
        {
// logical stuf
}


Calling from jquery :

HTML
$(document).ready(function(){
$("#GetUserDetails").click(function(){

var searchRequest = new Object(); 
    searchRequest.fileName = "Chris";  

		$.ajax({
			type:"POST",
			url:"http://localhost/TestAndroidWcfService/Service1.svc/UplodImageAndroid",
			ProcessData:false,			
			contentType:"application/json;charset=utf-8",
			dataType:"json",
			data:searchRequest,			
			success:function(data){
			alert("Success:"+ data);
			},
			error:function(xhr,status,error){
			alert("Error"+error);
			}
		});
		alert("Hi2");
	});
});



Here I am getting error message as Bad Request!!

How to make it work? What is the mistake in code?

Please suggest.

Thanks
Avinash
Posted

VB
$.ajax({
                        type:"POST",
                        url:"http://localhost/TestAndroidWcfService/Service1.svc/UplodImageAndroid",
                        ProcessData:false,
                        contentType:"application/json;charset=utf-8",
                        dataType:"json",
                        data: "{'custImg':'+searchRequest +'}",
                        success:function(data){
			alert("Success:"+ data);
			},
			error:function(xhr,status,error){
			alert("Error"+error);
			}
                    });



Please note Im not sure but, the data parameter name "custImg" and Webservice receiving parameter name "custImg" must be same.
   
Comments
Avinash6474 2-Nov-12 2:46am
   
Hi Sanjeev,

I have one more question for u,
How we can pass javascript array to the json object data,
I am doing something like this but not working-

var mycars = new Array();
mycars[0]="avinash";
mycars[1]="Sudhakar";

alert(mycars[0]);
alert(mycars[1]);


$.ajax({
type:"POST",
url:"http://localhost/TestAndroidWcfService/Service1.svc/testUploadImage",
ProcessData:false,
data:'{"fileName":'+mycars+'}',

If I passed the static values it is acception but after passing array variable it shows error.

Thanks for your suggessions.

--Avinash
the type of receiving parameter of your services must be an Array. i mean the "fileName" is the parameter in web services so, it must be an Array type.

Please check the following link.
http://stackoverflow.com/questions/11188315/javascript-array-as-wcf-webservice-parameter[^]

i hope it helps you.
   
Following steps I followed to pass JSON object to WCF service:
1) format of JSON object as WCF data contract
2) pass the jsonText to wcf service

HTML
var jsonText = JSON.stringify({"fileName": fileName,"ImageBytes":strings});
      
      
      $.ajax({
          type:"POST",          
     url:"http://localhost/TestWcfService/Service1.svc/UplodFile",
          data:jsonText,
          contentType:"application/json;charset=utf-8",
          dataType:"json",
          success:function(data){
          alert("success"+data);
          //alert("Success:"+ data );
          },
          error:function(error){
          alert("Error:"+ error.message );
          }
         });


Thanks,
Avinash
   
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