开发者

how to post a json from javascript?

开发者 https://www.devze.com 2023-01-22 18:15 出处:网络
i want to send a json string from a html webpage using javascript to a WCF.. is there any good tutorial for that?

i want to send a json string from a html webpage using javascript to a WCF.. is there any good tutorial for that?

this is the code i am using

<head> 
   <title>Test</title> 
   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
   <script type="text/javascript" src="http://www.json.org/json2.js"></script> 
   <script type="text/javascript"> 
     $(function() { 
      JSONStringer json = new JSONStr开发者_StackOverflow社区inger()

                      .object()   
                      .key("cno").value("2000")
                      .key("cname").value("HI")
                      .key("cmail").value("HI")
                      .key("cphno").value("9292")
                      .key("cmailtype").value("Home")
                      .key("cphnotype").value("Office")
                      .key("clientno").value("1")

                    .endObject();
       var dat = JSON.stringify(json.serializeArray()); 

       alert("I am about to POST this:\n\n" + dat); 

       $.post( 
         frm.attr("action"), 
         dat, 
         function(data) { 
           alert("Response: " + data); 
         } 
       ); 
     }); 
   </script> 
</head> 

let me know where i have to post it to a particular service.. something like specifying the URL


I think you are mixing up java with javascript. Despite their names they are not related to each other in any way. As far as I know, JSONStringer doesn't exist in javascript nor jquery. JSON stands for JavaScript Object Notation, so that means it is very native to the javscript languages (with some subtle differences). Since it's so close it is very easy to parse Json in javascript.

Also, javascript is a dynamically typed language so supplying the type as you did normally results in a parse error. Use firebug or the Chrome console when your code doesn't work. You will see an error when the browser was unable to parse your code.

for the serialization you probably want to use (in a browser that supports JSON and/or with json2.js)

var dat = JSON.stringify({
  cno: 2000,
  cname: 'HI',
  cmail: 'HI',
  cphno: '9292',
  cmailtype: 'home',
  cphnotype: 'Office',
  clientno: 1
});

The url goes where you have put frm.attr("action"). I don't see where you create the frm object. I don't think you need a JQuery object for that, document.getElementById is supported in all major browsers and I bet it is faster too.

var myForm = document.getElementById('myformid');
$.post( 
  myForm.action, 
  dat, 
  function(data) { 
    alert('Response: ' + data); 
  } 
); 

Also as far as I know, postdata has to be in query parameter format so perhaps you need to put something like

'myData=' + dat,

Copy/pasting code from the web can get things started quickly but a lot of javascript programmers forget that you have to understand the language first. Don't just blindly copy code, try to understand what happens. Try to solve problems first without a library and discover where you actually need a library.


http://www.learn-ajax-tutorial.com/PassingData.cfm#JSON

  1. Get json.js.
  2. Encode array or object in to json.
  3. Do Ajax.Request to post the json string. Simplez!

That is pretty much all you need, not sure about the WCF side but if it is a proper webservice then all you need is the descriptor to figure out the function names and their parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消