javascript - Unterminated string literal in JSON string with values from SQLServer with HTML tags -
i have been trying solve 'unterminated string literal error' in firebug when select 'breast' dropdown on page - http://wftp.whclpreview.com/search.html , hit on submit button. error points quotes have highlighted in bold -
specialgroups":"a","nationalaudits":"na","specialinterests":"
basically error result of $.ajax call on asp.net function returns data sqlserver database (containg field called specialinterests) in json format. database attached admin alows users format text via ckeditor plugin.
if text simple ok if contains html tags or line breaks above error.
any suggestions welcome, thanks.
if using ajax, can pass object literal or build querystring de data: e.g.
//if variable contains html string, //ensure encode before sending request var note = " <strong>important:</strong><br/> additional notes. "; note = encodehtml(note); //object literal var params = { "date": date, "expiry": expiry, "priority": priority, "note": note }; //query string append url var paramsurl = buildurlparams(params).join("&"); $.ajax({ type: "post", url: "myservice/client", //ugly way //data: "date="+date+"&expiry="+expiry+"&priority="+priority+"¬e="+note, //passing data literal object data: params, //passing data querystring //data: paramsurl, success: function(data){ alert(data); } });
//creates array of parameters build url querystring //@obj: object build array of parameters function buildurlparams(obj) { return $.map(obj, function(value, key) { return key + "=" + value; }); }
//encodes hmtl respective html entities //@text: html string encode function encodehtml(text) { var div = document.createelement("div"); if ("innertext" in div) div.innertext = text; else div.textcontent = text; return div.innerhtml.replace(/^\s+|\s+$/, ""); }
Comments
Post a Comment