AngularJS - Special Characters in JSON and Coldfusion Request

Deadly 提交于 2019-12-11 17:15:16

问题


I'm trying to use a JSON string for doing an INSERT in a database . I'm using AngularJS and Coldfusion for doing that.

Thanks to a form I retrieve data, I create an object in the service (controller) and thanks to a factory I do an $http.post. My problem concerns the string sent to it: the string contains special chars (for instance "&" and I don't know how to treat this kind of issue.

Here an example of the string sent:

jsStruct={"LASTNAME":"Nämé","FIRSTN%a£öME":"TestFirstName","PHONENUMBER":48484488,"EMAIL":"tes.test@test.test","COMPANY":"Test & Comp"}

My controller:

app.controller('ctrlAddContacts', function ($scope, ContactService){

    // WHEN SUBMITTING THE FORM -> SEND THE DATA STRING 
    $scope.submitForm = function(contact){
        if($scope.ContactForm.$valid){

            // CALL THE FACTORY -> SEND THE DATA STRING 
            ContactService.addNewPerson(contact).success(function(Person){
                $scope.ContactForm.$setPristine();
                $scope.contact= Person;     

            });     
        }
    }
});

My Factory:

app.factory('ContactService', function($http){

    var factory={};

    factory.addNewPerson=function(objContact){

        return $http.post('http://myapp/contacts.cfc?method=addNewPerson&jsStruct=' + JSON.stringify(objContact))
    };  

    return factory;

})

In my Coldfusion component "contacts.cfc":

    <cffunction name="addNewPerson" access="remote" returnformat="JSON" output="no">    
        <cfargument name="jsStruct" type="string" required="true">
        <cfset var cfStruct=DeserializeJSON(jsStruct)>

        ..................

    </cffunction>   

I obtain this error on the server because in my string there is "COMPANY":"Test & Comp":

JSON parsing failure: Unexpected end of JSON string 

Could you please help me to solve this problem and prevent special characters used by Coldfusion and Oracle as &, #, ', " and others?

Thank you in advance.


回答1:


I just tried parsing your JSON. It worked for me. I guess the JSON parser used on server end do not support special char.

var jsStruct={"LASTNAME":"Nämé","FIRSTN%a£öME":"TestFirstName","PHONENUMBER":48484488,"EMAIL":"tes.test@test.test","COMPANY":"Test & Comp"};

var parsedJSON = JSON.parse(JSON.stringify(jsStruct));
console.log(parsedJSON,"parsedJSON")

Edit 1:

As per your error message: JSON parsing failure: Unexpected end of JSON string

You're trying to parse a string as JSON, and the string isn't JSON. Error is not for special character.




回答2:


In fact we asked to the DBA to change the DSN to use different driver and the problem has been solved



来源:https://stackoverflow.com/questions/44773766/angularjs-special-characters-in-json-and-coldfusion-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!