Steven Zack
Steven Zack

Reputation: 5104

how to pass json in jQuery $.ajax method?

I have following json data which I want to pass it to server using $.ajax. I tried many times, nothing returned, which is suppose to return another json object. Please help

$(function() {
        var parameters = {
            "firstName": "Saumil",
            "lastName": "Jhaveri",
            "email": "[email protected]",
            "address": "650+Townsend+St,+St.+325",
            "city": "San+Francisco",
            "state": "California",
            "zipCode": "94103",
            "country": "United+States",
            "phone": "312&375&1884",
            "industry": "Accounting",
            "organization": "Citrix",
            "jobTitle": "Software+Engineer",
            "purchasingTimeFrame": "1&3+months",
            "roleInPurchaseProcess": "Decision+Maker",
            "numberOfEmployees": "1&20",
            "questionsAndComments": "No+Comments!",
            "responses": [{
                "questionKey": 152,
                "responseText": "Fantastic!"
            }, {
                "questionKey": 151,
                "answerKey": 152
             }]
            };
          var jsonData=$.toJSON(parameters);  
    $.ajax({
        type: "POST",
        url: "https://api.citrixonline.com/G2W/rest/organizers/2934047/webinars/439546160/registrants?oauth_token=57f9454c6aecec65adef1ca66cbfde02",
        data: jsonData,
            contentType: "application/json",
            dataType: "json",
            success: function(data) {
                $('#key').html(data.registrantKey);
                $('#url').html(data.joinUrl);
            }
        });
    });

Here's the div that I want to display returned data.

<div id="key"></div>
<div id="url"></div>

I have no control on the url that I send data to, but it is says I'll receive response something as follows:

     HTTP/1.1 201 OK
 Content-Type: application/json
 {
 "registrantKey":5678,
 "joinUrl":"https://www1.gotomeeting.com/join/123456789/5678"
 }

Upvotes: 0

Views: 249

Answers (1)

Jasper
Jasper

Reputation: 75993

The $.toJSON method will return a string formatted as JSON. This string can be the value of a POSt variable if you give it a key:

$.ajax({
    type: "POST",
    url: "https://api.citrixonline.com/G2W/rest/organizers/2934047/webinars/439546160/registrants?oauth_token=57f9454c6aecec65adef1ca66cbfde02",
    data: 'jsonData=' + jsonData,
        contentType: "application/json",
        dataType: "json",
        success: function(data) {
            $('#key').html(data.registrantKey);
            $('#url').html(data.joinUrl);
        }
    });

I'm assuming that the $.toJSON method you are using is part of this plugin: http://code.google.com/p/jquery-json/

Upvotes: 1

Related Questions