title: JSON.parse: unexpected character error with ajax and json
body: <p>I'm having some problem with Json. I want to send a json object to a database on a server and in return get a json result. I'm working with KnockoutJS, jQuery and struts-2.</p>
  
  <p>This is javascript code:</p>
  
  <pre><code>function(ko, dataservice, config, router, messenger, Utente, Vario) {
      var
              logger = config.logger,
              utente = new Utente(),
              vario = new Vario(),
              isValid = ko.computed(function() {
          return utente.username() &amp;&amp; utente.password() &amp;&amp; utente.email();
      }),
              activate = function(routeData, callback) {
      },
              registrazione = function() {
          if (isValid()) {
              if (utente.password() === vario.password()) {
  
                  dataservice.servizisegreteria.registrazione({
                      success: function(result) {
                          if (result.esito === 'OK') {
                              router.navigateTo(config.hashes.registrazione);
                              logger.info("Controlla la tua casella mail!");
                          }
                          else {
                              logger.error("Salvataggio non riuscito!");
                          }
  
                      },
                      error: function(result) {
                          logger.error("Controllare che i dati siano giusti!");
                      }
  
  
                  }, $.toJSON({
                      'utente': {
                          'username': utente.username(),
                          'password': utente.password(),
                          'email': utente.email(),
                          'dataIscrizione': new Date()
                      }
                  }));
  
  
              }
  
              else
                  logger.error("Le Password devono combaciare!");
          }
          else
              logger.error("Inserire tutti i  dati!");
      };
  
  
      return {
          activate: activate,
          utente: utente,
          vario: vario,
          registrazione: registrazione
      };
  }
  </code></pre>
  
  <p>This is code for ajax request:</p>
  
  <pre><code>var init = function() {
          amplify.request.define('registrazione', 'ajax', {
              url: '/VenditaGioielli/gestioneSegreteria/nuovoUtente.action',
              dataType: 'json',
              type: 'POST',
              contentType: 'application/json; charset=utf-8'
          });
      },
  
              registrazione = function(callback, data) {
          return amplify.request({
              resourceId: 'registrazione',
              data: data,
              success: callback.success,
              error: callback.error
          });
      },
  
      init();
  
      return {   
          registrazione: registrazione
  
      }
  }
  </code></pre>
  
  <p>This is html code:</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;%@taglib prefix="s" uri="/struts-tags" %&gt;
  &lt;script type="text/javascript"&gt; 
  &lt;/script&gt;
  &lt;section id="registrazione-view" class="view"&gt;
  &lt;div id="container"&gt;
  
      &lt;div id="contenuto"&gt;
          &lt;div class="formGioiello"&gt;  
  
              &lt;div class="gruppoInput"&gt;
  
                  &lt;div&gt;
                      &lt;label&gt;Username&lt;/label&gt;
                      &lt;input type="text" placeholder="Username" data-bind="value: utente.username"  required/&gt;  
                  &lt;/div&gt; 
                  &lt;div&gt;
                      &lt;label&gt;Password&lt;/label&gt;
                      &lt;input type="password" placeholder="Password" data-bind=" value : utente.password" required/&gt;
                  &lt;/div&gt;
                  &lt;div&gt;
                      &lt;label&gt;Ripeti Password&lt;/label&gt;
                      &lt;input type="password" placeholder="Ripeti Password" data-bind="value :vario.password" required/&gt;
                  &lt;/div&gt;
                  &lt;div&gt;
                      &lt;label&gt;Email&lt;/label&gt;
                      &lt;input type="email" placeholder="Email" data-bind="value :utente.email" required/&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div&gt;
                  &lt;button data-bind="click: registrazione, jqueryui: 'button'"&gt;Registrati!&lt;/button&gt;
              &lt;/div&gt;
          &lt;/div&gt; 
  
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>this is struts.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
  
  &lt;struts&gt;
  &lt;constant name="struts.devMode" value="true" /&gt;
  &lt;package name="segreteria" namespace="/gestioneSegreteria" extends="json-default"&gt;
  
      &lt;interceptors&gt;
          &lt;interceptor name="segreteriainterceptor" class="it.lt.venditagioielli.gestioneSegreteria.web.GestioneSegreteriaInterceptor" /&gt;
          &lt;interceptor-stack name="gestionesegreteriastack" &gt;
              &lt;interceptor-ref name="servletConfig"/&gt;
              &lt;interceptor-ref name="segreteriainterceptor"/&gt;
          &lt;/interceptor-stack&gt;     
      &lt;/interceptors&gt;
      &lt;default-interceptor-ref name="gestionesegreteriastack"/&gt;
  
      &lt;global-results&gt;
          &lt;result name="nonloggato" type="redirect"&gt;/index.jsp&lt;/result&gt;
      &lt;/global-results&gt;
  
      &lt;action name="nuovoUtente" class="it.lt.venditagioielli.gestioneSegreteria.web.GestioneUtenteAction" method="inserisci"&gt;
          &lt;interceptor-ref name="json"&gt;
              &lt;param name="contentType"&gt;application/json&lt;/param&gt;
          &lt;/interceptor-ref&gt;
          &lt;interceptor-ref name="gestionesegreteriastack" /&gt;
          &lt;result type="json"&gt;
              &lt;param name="root"&gt;esito&lt;/param&gt;
          &lt;/result&gt;
          &lt;/action&gt;
      &lt;/package&gt;
  &lt;/struts&gt;
  </code></pre>
  
  <p>And this is the action code:</p>
  
  <pre><code>  package it.lt.venditagioielli.gestioneSegreteria.web;
  
    import com.opensymphony.xwork2.Action;
    import it.lt.venditagioielli.gestione.model.Esito;
    import it.lt.venditagioielli.gestione.model.Utente;
    import it.lt.venditagioielli.gestioneSegreteria.servizi.ServiziSegreteria;
  
    public class GestioneUtenteAction {
      private Utente utente = new Utente();
      private Esito esito = new Esito();
      private ServiziSegreteria serviziSegreteria;
      private String idUtente;
  
      public String inserisci() {
          try {
              serviziSegreteria.inserisciNuovoUtente(utente);
              serviziSegreteria.inviaMailConferma(utente);
              esito.setEsito("OK");
          } catch (Exception ex) {
              esito.setEsito("KO");
          }
  
          return Action.SUCCESS;
      }
  
      public Esito getEsito() {
          return esito;
      }
  
      public void setEsito(Esito esito) {
          this.esito = esito;
      }
  
      public Utente getUtente() {
          return utente;
      }
  
      public void setUtente(Utente utente) {
          this.utente = utente;
      }
  
      public void setServiziSegreteria(ServiziSegreteria serviziSegreteria) {
          this.serviziSegreteria = serviziSegreteria;
      }
      public String getIdUtente() {
          return idUtente;
      }
  
      public void setIdUtente(String idUtente) {
          this.idUtente = idUtente;
      }
    }
  </code></pre>
  
  <p>I'm getting  this error while getting data from database: </p>
  
  <blockquote>
    <p>JSON.parse: unexpected character error</p>
  </blockquote>
  
  <p>in fact, netbeans doesn't even get to the <code>GestioneUtenteAction</code> class on debug mode! </p>
  
  <p>The json object sent is</p>
  
  <pre><code>{"utente":{"username":"luca","password":"l","email":"ho@ho.it","dataIscrizione":"2013-11-04T13:30:09.575Z"}}
  </code></pre>
  
  <p>Does anyone have a clue how to fix this?</p>
  
  <p>EDIT: the action isn't hit, and the answer from the server is my html page, instead of a json object. This html is  given to the parseJSON function of jquery. I came to this result by comparison whit another working action</p>