0

I have an array of colors and sizes, and some other data like product_name and images, I am able to send image files and product_name but when I attach color_and_sizes array then it says 500 error

Here is my Code :(Angularjs and Laravel Controller ) Basically I want to save all the details in the database but I am unable to save the color and sizez details.

$scope.admin_add_product=function(){
	  var product_name=$scope.txtProductname;
	  var product_description=$scope.tarDescription;
	  var price=$scope.txtprice;
	  var unit_in_stock=$scope.txtunitinstock;
	  var unit_weight=$scope.txtweight;
	  var file = $scope.front_image;
	  var file2=$scope.back_image;
	  var file3=$scope.left_side_image;
	  var file4=$scope.right_side_image;
	  var main_image=$scope.main_image;
	  var large_image=$scope.large_image;
	  var category_id=$scope.selectedCategory.category_id;
	  var sub_category_id=$scope.selectedSubCategory.sub_category_id;
	  var sub_sub_category_id=$scope.selectedSubSubCategory.sub_sub_category_id;
	  var product_key=$scope.addProductOption;
	  var tags=$scope.txt_tags;
	  var aditional_information=$scope.txt_aditional_information;
	  var temp_color_and_size=[];
	  temp_color_and_size=$scope.color_and_size;
	  console.log(temp_color_and_size);
	  var uploadUrl = 'http://web.com/app/add_product';
	  fileUpload.uploadFileToUrl(file,file2,file3,file4,main_image,large_image, product_name,product_description,price,category_id,sub_category_id,sub_sub_category_id,unit_in_stock,unit_weight,product_key,tags,aditional_information ,temp_color_and_size, uploadUrl);
	  
	}
  
  /* ---------------------------------------------------- Color and Sizes -------------------------------------------------- */
			$scope.color_and_size=[];
			$scope.Add=function(){
				var c_and_s=[];
				c_and_s.color_name = document.getElementById('cl1').value;
				var s;
				if($scope.sizes!=null)
				{
					for(var i=0;i<$scope.sizes.length-1;i++)
					{
						if($scope.sizes[i+1]>=0 && $scope.sizes[i+1]<=9)
						{
							
						}
						else
						{
							if($scope.sizes[i+2]>=0 && $scope.sizes[i+2]<=9)
							{
								
							}
							else
							{
								$scope.sizes = $scope.sizes.substring(0, i+1);
							}
						}
					}
					console.log($scope.sizes);
					$scope.size=$scope.sizes.split(',');
					c_and_s.sizes=$scope.size;
				}
				$scope.color_and_size.splice(0,0,c_and_s);
				console.log($scope.color_and_size);
				document.getElementById('cl1').value="#000000";
				$scope.sizes="";
			}
      
      
      myApp.service('fileUpload', ['$http', function ($http) {
     this.uploadFileToUrl = function(file,file2,file3,file4,main_image,large_image,product_name,product_description,price,category_id,sub_category_id,sub_sub_category_id,unit_in_stock,unit_weight,product_key,tags,aditional_information,temp_color_and_size, uploadUrl){
		console.log(product_name);
		console.log(product_description);
		console.log(price);
		console.log(unit_in_stock);
		console.log(unit_weight);
		console.log(file);
		console.log(temp_color_and_size);
		console.log(product_key);
		console.log(tags);
		console.log(aditional_information);
		
		var payload = new FormData();
		payload.append("product_name", product_name);
		payload.append('product_description', product_description);
		payload.append('price', price);
		payload.append('unit_in_stock', unit_in_stock);
		payload.append('unit_weight', unit_weight);
		payload.append('file', file);
		payload.append('file2', file2);
		payload.append('file3', file3);
		payload.append('file4', file4);
		payload.append('main_image', main_image);
		payload.append('large_image', large_image);
		payload.append('product_key', product_key);
		payload.append('tags', tags);
		payload.append('aditional_information', aditional_information);
		payload.append('temp_color_and_size', temp_color_and_size);
		
		return $http({
			url: uploadUrl,
			method: 'POST',
			data: payload,
			//assign content-type as undefined, the browser
			//will assign the correct boundary for us
			headers: { 'Content-Type': undefined},
			//prevents serializing payload.  don't do it.
			transformRequest: angular.identity
		})
		.then(function (response){
			alert('Product Added Successfully');
			window.location = "http://web.com/add_product"
		   },function (error){
				
		   });
	}
}]);

Please help me to resolve this problem.

1 Answer 1

0

Your question is not clear to me but I think the problem is that all properties of the formData object apart from temp_color_and_size are string type and temp_color_and_size is a javascript object. This might solve your problem.

payload.append('temp_color_and_size', JSON.stringify(temp_color_and_size);

You have to deserialize JSON string on the server side.

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

4 Comments

Still give POST web.com/app/add_product 500 (Internal Server Error)
Can you connect me over teamviewer?
@SurajKumar It depends on what the server expects for these values. It is the one throwing the error, you have to find out why.

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.