2

I'm trying to send a JSON string from an ajax call to an ASP.NET api. The JSON string arrives but it is truncated at an ascii code.

So how can I allow ascii code in my parameter string?

Here is the related code:

function sendBewerkVacature(id) { var velden = document.getElementsByClassName("veld"); var veldenArray = "{'" + velden[0].name + "' : '" + velden[0].value+"'"; for (var i = 1; i < velden.length; i++) { veldenArray = veldenArray + ", '" + velden[i].name + "' : '" + velden[i].value+"'"; } veldenArray = veldenArray + "}"; var JSONVelden = JSON.stringify({ "velden": veldenArray }); console.log(JSONVelden); $.ajax({ url: "http://localhost:26454/api/Vacatures/PostEditVacature?id=" + id + "&velden=" + JSONVelden, type: "POST", statusCode: { 200: function (data) { alert("Geslaagd!"); } } }) } public HttpResponseMessage PostEditVacature(int id, string velden) { db.vacatures.Find(id); JavaScriptSerializer serializer = new JavaScriptSerializer(); vacatures vacature = (vacatures)serializer.DeserializeObject(velden); var response = new HttpResponseMessage(HttpStatusCode.OK); return response; } 

The string that is sent looks like this:

object a:{"velden":"{'FunctieTitel' : 'Natuurkunde', 'school_naam' : 'undefined', 'vak_naam' : 'Natuurkunde', 'aantal_uren' : '18-25', 'datum_ingang' : '16-2-2011', 'omschrijving' : '<p>Voor de afdeling havo/vwo klas 2 en 3 zijn wij op zoek naar een docent(e) Natuurkunde 2e graads voor 18 tot 25 lesuren per week.</p><p>&nbsp;</p><p>Locatie: Koninginnelaan te Vlaardingen</p><p>Klassen: leerjaar 2 en 3 havo/vwo</p><p>Salaris: inschaling in LB, arbeidsvoorwaarden conform CAO Voortgezet Onderwijs.</p><p>Ingangsdatum: 01-08-2011, tijdelijke aanstelling met uitzicht op een vaste aanstelling.</p>'}"} 

but in the controller it looks something like this:

{"velden":"{'FunctieTitel' : 'Natuurkunde', 'school_naam' : 'undefined', 'vak_naam' : 'Natuurkunde', 'aantal_uren' : '18-25', 'datum_ingang' : '16-2-2011', 'omschrijving' : '<p>Voor de afdeling havo/vwo klas 2 en 3 zijn wij op zoek naar een docent(e) Natuurkunde 2e graads voor 18 tot 25 lesuren per week.</p><p>} 
5
  • 1
    How exactly are you viewing the strings you've shown here? Commented Jul 1, 2016 at 14:32
  • take a look at this: stackoverflow.com/questions/14095247/… Commented Jul 1, 2016 at 14:33
  • I place a break point right under : public HttpResponseMessage PostEditVacature(int id, string velden) { Commented Jul 1, 2016 at 14:35
  • What code did you use and what was the result? ` &#38;` is not a valid ASCII code for JSON - JSON doesn't have ASCII codes. JSON only allows Unicode in the form \u1234. Post the code you used, the original text and the result, so we can reproduce the problem Commented Jul 1, 2016 at 14:36
  • Please put all of this into the question rather than in the comments. Commented Jul 1, 2016 at 14:51

1 Answer 1

1

So you're sending the json in a query string.

Your querystring looks something like:

?id=1234&velden={'vacancy_name': 'clown', 'vacancy_description':'juggle &#38; be funny'} 

So now your querystring contains an unexpected &, signifying the end of one querystring parameter and the start of the next.

You need to

encodeURIComponent(JSONVelden) 

before you add it to the querystring.

Even better, learn how to post JSON rather than sending it in query string parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That fixed it. Thanks a lot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.