I need to pass a JSON object to javascript / ajax method to call WCF. How can i do this?

So, I was looking for many different examples of this. Most people use the JSON serializer / deserializer or the stringify method. I tried to try this, and unfortunately I always get an error message from the Google Chrome debugger console saying: Uncaught ReferenceError: JavaScriptSerializer is not defined or something like that.

The following is the actual method in which I try to perform a PUT operation using the information.

function insertjsonMyUser(person) { document.write("Inside the parameter based insertion method<br/>"); alert("Entered the method, right before the ajax call"); $.ajax({ type: "PUT", url: 'http://localhost/MyService/Service.svc/json/CreateUserInfo', contentType: "application/json", data: {"User" : person.User ,"Pword" : person.Pword,"FName" : person.FName,"LName" : person.LName,"JobTitle" : person.JobTitle,"CompanyName" : person.CompanyName,"CompanyBranch" : person.CompanyBranch,"PhoneNum" : person.PhoneNum,"Email" : person.Email}, beforeSend: function(xhr) { xhr.setRequestHeader('Authorization', 'default default'); } }); alert("The user has been created according to your specifications"); } 

I want the person to be a JSON object using the specified keys that I have under the data. I have successfully made a similar method, but I do not want to use a hard JSON object, for example:

 {"User" : "Zuser" ,"Pword" : "password","FName" : "firstname","LName" : "lastname","JobTitle" : "jobtitle","CompanyName" : "companyname","CompanyBranch" : "companybranch","PhoneNum" : 3123123123,"Email" : "email"} 

The JSON object that I use in the hard-coded version is the object above. Is there something I am missing to use stringify or serialize methods? Also, is there an easier way than the ones I missed during the search hours?

Finally, I start Windows 7 Enterprise using IIS hosting and using Visual Studio 2010 for editing. My service is working fine. I successfully tested it in Fiddler.

+4
source share
3 answers

So, I finally figured out my own question. The reason the actual parameter passing did not work was single. When I declared my actual JSON object, I declared it as:

 {"User" : "Zuser" ,"Pword" : "password","FName" : "firstname","LName" : "lastname","JobTitle" : "jobtitle","CompanyName" : "companyname","CompanyBranch" : "companybranch","PhoneNum" : 3123123123,"Email" : "email"} 

I really needed to declare it as:

 '{"User" : "Zuser" ,"Pword" : "password","FName" : "firstname","LName" : "lastname","JobTitle" : "jobtitle","CompanyName" : "companyname","CompanyBranch" : "companybranch","PhoneNum" : 3123123123,"Email" : "email"}' 

I assume that you can add single quotes to the actual object in the method itself, but you can just make the actual declaration, as I did to get around the problem.

+2
source

well

  contentType: "application/json", data: person, beforeSend: .... 

Will this work?

From http://api.jquery.com/jQuery.ajax/, the data can be an object or a string.

+1
source

It looks like there might be a bug in your WCF service not detecting the JavascriptSerializer. Are you going in one car and deploying it to another? They may not be synchronized using the .NET library.

Also, add the exact error message you get, which will be very helpful.

0
source

Source: https://habr.com/ru/post/1337199/


All Articles