0

Following code:

$.ajax(serviceUrl,
    {
        data:
            format: 'json'
            id: [2,3,4]
        success: (data) ->
            successCallback(data) if successCallback
        error: (error) ->
            failureCallback(error) if failureCallback
    })

Sends request with following query string parameters:

format:json
id[]:2
id[]:3
id[]:4

Is it possible to somehow avoid those brackets at the end of parameter?

  • 1
    possible duplicate of [How do I send an array using jQuery and the .ajax method without escaping the brackets?](http://stackoverflow.com/questions/8934961/how-do-i-send-an-array-using-jquery-and-the-ajax-method-without-escaping-the-br) – Felix Kling May 07 '14 at 14:07

1 Answers1

0

By default jquery ajax uses GET method if the 'type' parameter not set. So, the 'data' value should be converted into the part of the serviceURL. One of the popular way to send array as a parameter is to convert it into string like: id[]=2&id[]=3&id[]=4

You may send parameters as JSON object or other http content-type, but using other methods, like POST or PUT.

Semantics of GET is to retrieve a resource, POST - to create a new resource, PUT - create or modify if exists, etc. You may use something like this to send and receive data in JSON format:

data = 
  format: 'json'
  id: [2,3,4]

$.ajax
  url: serviceUrl
  type: 'post'
  data: JSON.stringify(data)
  contentType: 'application/json'
  dataType: 'json'
NBR
  • 460
  • 4
  • 3