Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi All,
 
I want to pass JSON Object to WCF service,
Here is my service method:
 [OperationContract]
        [WebInvoke(Method="POST", RequestFormat=WebMessageFormat.Json,     ResponseFormat=WebMessageFormat.Json,        UriTemplate = "/UplodImageAndroid")]
        bool uploadImageAndroid(CustomImage custImg);
 
public bool uploadImageAndroid(CustomImage custImg)
        {
// logical stuf
}
 
Calling from jquery :
 
$(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 1-Nov-12 20:14pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

$.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.
  Permalink  
Comments
Avinash6474 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
 
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
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 273
2 Aajmot Sk 224
3 Marcin Kozub 210
4 Richard MacCutchan 200
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 17 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100