0

I have a problem, I did set field form to array. Example : Field dfp_interest in response is []. So, i must set my data to array right ? I did and success in console.log. Result like --> ["5","6"] but when I process in angular.fromJson and i send with $http, my params dfp_interest is string not array. Am I wrong ? Give me solution or i do some mistakes ?

This is my process to send params in API

var a = angular.fromJson({
            "data" : "{\"username\": \"newshubid\", \"data\":{\"_id\": \""+ $scope.id +"\",\"label\": \""+ $scope.label +"\",\"active\": \""+ $scope.active +"\",\"parent_id\": \""+ $scope.parent_id +"\",\"level\": \""+ $scope.level +"\",\"meta_title\": \""+ $scope.meta_title +"\",\"meta_description\": \""+ $scope.meta_description +"\", \"meta_keyword\": \""+ $scope.meta_keyword +"\", \"dfp_interest\": \""+ $scope.tes +"\"}}"
        });

        var param = $.param(a);

        HttpService("POST", url, param, function(response){
            alert(response.message);
        });

This is example my data array for dfp_interest

$scope.tes = [5, 6];

$scope.testarray = $scope.tes.join("");
1
  • You don't understand what JSON is. What you pass into angular.fromJson is not a JSON. Commented Mar 9, 2017 at 12:34

1 Answer 1

1

angular.fromJson accepts a string argument, with that said your code should be;

var data = {username: 'newshubid', 
            data: { id: $scope.id, 
                    label: $scope.label, 
                    active: $scope.active,
                    parent_id: $scope.parent_id,
                    level: $scope.level,
                    meta_title: $scope.meta_title,
                    meta_description: $scope.meta_description,
                    meta_keyword: $scope.meta_keyword,
                    dfp_interest: $scope.tes }
           };

    var a = angular.fromJson(JSON.stringify(data));
Sign up to request clarification or add additional context in comments.

6 Comments

Variables interpolation $scope.id, etc.
But "id" and $scope.id no need ("") ?
@userpr JSON.stringify will do that for you
the console.log is object not string
So, formData in xhr is object Object and cant process to API
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.