Why axios request fail with 400? - axios

I'm new to Composition API and I don't know why I get http response 400. Payload in request looks same as json object. I have to send to an api a nested object. JSON object looks like this:
{
"contact": {
"email": "",
"firstname": "",
"lastname": "",
},
"description": "",
"preference": "",
"title": "",
"hobbies": [],
"maxSum": -17692428,
"minSum": 98485775,
"interests": [
{
"interest1": ""
},
{
"intereest2": ""
}
],
"passions": [
{
"pasion1": ""
},
{
"passion2": ""
}
]
}
My template is:
<template>
<form class="form" novalidate="">
<div class="form__header">
<h3 class="form__title">Title</h3>
</div>
<div class="form__section">
<label for="title">Title*</label>
<input
type="text"
v-model="title"
/>
</div>
<div class="form__section">
<label for="preference"> Preference*</label>
<textarea
type="text"
v-model="preference"
/>
</div>
<div class="form__section">
<label for="hobbies"> Programming languages*</label>
<textarea
type="text"
v-model="hobbies"
/>
</div>
<div class="form__section">
<label for="interests"> interests*</label>
<textarea
type="text"
v-model="interests"
/>
</div>
<div class="form__section">
<label for="passions"> passions*</label>
<textarea
type="text"
v-model="passions"
/>
</div>
<div class="form__section">
<div> Job mobility*</div>
<select v-model="preference">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>
<div class="form__section">
<label for="minSum"> Minimum Payment *</label>
<input
type="number"
v-model="minSum"
/>
</div>
<div class="form__section">
<label for="maxSum"> Maximum Payment *</label>
<input
type="number"
v-model="maxSum"
/>
</div>
<div class="form__section">
<label for="name"> name*</label>
<input
type="text"
placeholder=" Name"
v-model="contact.firstname"
/>
</div>
<div class="form__section">
<label for="last name"> last name *</label>
<input
type="text"
placeholder=" Last Name"
v-model="contact.lastname"
/>
</div>
<div class="form__section">
<label for="email"> email *</label>
<input
type="email""
placeholder=" Email"
v-model="contact.email"
/>
</div>
<div class="form-buttons">
<div class="form__section">
<button
#click.prevent="save()"
id="save"
class="form__submit"
type="submit"
data-inline="true"
>
Save
</button>
</div>
</div>
</form>
</template>
And script tag:
<script>
import { reactive, toRefs } from "vue";
import { ref } from "vue-demi";
export default {
setup() {
const handleSave = reactive({
title: "",
description: "",
hobbies: [],
passions: [],
interests: [],
description: "",
maxSum: "",
minSum: "",
contact: {}
});
const isSaveAdded = ref(false)
const addSave = async () => {
const body = {}
body.title = handleSave.title
body.description = handleSave.description
body.hobbies = handleSave.hobbies.toString().split(",").map(value => {return {hobby:value }})
body.technologies= handleJob.technologies.toString().split(",").map(value => {return {techName:value }})
body.passions = [handleSave.passions]
body.interests = handleSave.interests
body.maxSum = handleSave.maxSum
body.minSum = handleSave.minSum
body.contact = handleSave.contact
const response = await axios.post("/foo", body,
{
headers: {
///////////////////////
}
});
handleSave.value = response.data;
isSaveAdded.value = true
};
return {
...toRefs(handleSave),
addSave,
isSaveAdded
};
}
};
</script>
Why request is not successful and what should it look like? Thank you

Related

Razor form is not showing all inputs (#Html.EditorFor) even after copy/pasting code

Every input is showing and working with the exception of Last Name and City. I've copy/pasted from two separate inputs in an attempt to get them all to work. I can't find a difference in the code anywhere but when I debut and inspect it, there is a label and verification but not input for both. I'm at a loss as to what is causing the inputs to be deleted for only these two elements.
<div class="form-horizontal">
<div class="row">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<!-- Customer Information -->
<div class="col-md-6 col-md-offset-2">
<div class="form-group">
#Html.LabelFor(model => model.C_First_Name, "First Name", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.C_First_Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.C_First_Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.C_Last_Name, "Last Name", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.C_Last_Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.C_Last_Name, "", new { #class = "text-danger" })
</div>
</div>
#*<div class="form-group">
#Html.LabelFor(model => model.C_Last_Name, "Last Name", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.C_Last_Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.C_Last_Name, "", new { #class = "text-danger" })
</div>
</div>*#
<div class="form-group">
#Html.LabelFor(model => model.C_Address, "Address", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.C_Address, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.C_Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.C_City, "City", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.C_City, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.C_City, "", new { #class = "text-danger" })
</div>
</div>
#*<div class="form-group">
#Html.LabelFor(model => model.C_City, "City", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.C_City, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.C_City, "", new { #class = "text-danger" })
</div>
</div>*#
<div class="form-group">
#Html.LabelFor(model => model.C_State, "State", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.C_State, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.C_State, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.C_Country, "Country", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.C_Country, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.C_Country, "", new { #class = "text-danger" })
</div>
</div>
</div>
<!-- Order Information -->
<div class="col-md-6 col-md-offset-2">
<div class="form-group">
#Html.LabelFor(model => model.Total, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Total, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Total, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Product_ID, "Product_ID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("Product_ID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.Product_ID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group text-right">
<div class="col-md-10">
#Html.ActionLink("Back to List", "Index")
<input type="submit" value="Create" class="btn btn-default ml-5" />
</div>
</div>
</div>
</div>
</div>
Firstly,I test your code,it can work.If your EditFor not work,you can try to use input and span:
<div class="form-group">
#Html.LabelFor(model => model.C_Last_Name, "Last Name", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<input class="form-control text-box single-line" id="C_Last_Name" name="C_Last_Name" type="text" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="C_Last_Name" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.C_City, "City", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<input class="form-control text-box single-line" id="C_City" name="C_City" type="text" value="">
<span class="field-validation-valid text-danger" data-valmsg-for="C_City" data-valmsg-replace="true"></span>
</div>
</div>
result:
It turns out I was looking in the wrong place. The problem was not in my razor code but in the database. The data types for City and State were mistyped and this was causing them to not show in the debug. I corrected this in MS Sequel Studio by changing the fields in the database diagram. I used the database first Entity Framework setup and changing column specifications in the database threw Visual Studio for a loop so I tried dropping the Orders table I had changed and re-adding it to the .edmx but it's connected to the Products table which then started producing errors. In the end I had to delete the entire .edmx in Visual Studio then right click and refresh from database to add back the tables I needed to get the code to run correctly. My apologies for my incoherent question.

get Aggregate Query Result in the Template File (Handlebars)

var agg = [
{$group: {
_id: null,
totalPrice: { $sum: "$itemPrice" },
total: {$sum: 1}
}}
];
Idea.aggregate(agg, function(err, logs){
if (err) {
return def.reject(err);
}
console.log(logs);
});
and here it is the template file
{{#each ideas}}
<div class="card card-body mb-2">
<h4>{{title}}</h4>
<p>{{details}}</p>
<a class="btn btn-dark btn-block mb-2" href="/ideas/edit/{{id}}">Edit</a>
<form method="post" action="/ideas/{{id}}?_method=DELETE">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" class="btn btn-danger btn-block" value="Delete">
</form>
</div>
{{else}}
<p>No video ideas listed</p>
{{/each}}

collections2 and meteor-cfs-autoform to upload and display image

I am using collections2 and meteor-cfs-autoform for schemas and to upload and display image using cfs:gridfs storage adaptor package but unable to show images in template.Can you please tell me where i am doing mistake and its solution.
collections.js
Recipes = new Mongo.Collection('recipes');
Reviews = new Mongo.Collection('reviews');
RecipesImages = new FS.Collection("recipesImages", {
stores: [new FS.Store.GridFS("recipesImages")]
});
RecipesImages.allow({
insert: function(userId, doc) {
return true;
},
update: function(userId, doc, fieldNames, modifier) {
return true;
},
download: function(userId) {
return true;
},
fetch: null
});
Recipes.attachSchema(new SimpleSchema({
name: {
type: String,
label: "Recipe Name",
max: 100
},
ingredients: {
type: [Object],
minCount: 1
},
"ingredients.$.name": {
type: String
},
"ingredients.$.amount": {
type: String
},
description: {
type: String,
label: "How to prepare ",
},
time: {
type: Number,
label: "Time (Minutes)",
},
image: {
type: String,
autoform: {
afFieldInput: {
type: "cfs-file",
collection: 'recipesImages',
label: 'Recipe Picture'
}
}
}
}));
recipes.js
Template.recipes.helpers({
'showRecipes':function(){
return Recipes.find();
},
'images': function () {
return RecipesImages.findOne({_id:id}) ;
}
})
recipes.html
<template name="recipes">
{{#each showRecipes}}
<div class=" col-md-4">
<div class="panel panel-default mb " >
<div class="panel-image">
<img src="{{this.url }}" class="panel-image-preview" />
</div>
<div class="panel-body">
<h4>{{name}}</h4>
<p>{{description}}</p>
</div>
<div class="panel-footer text-center" >
<p>its footer </p>
</div>
</div>
</div>
{{/each}}
</template>
Are you importing cfs:ui? If not, you should include it so that you can use the FS.GetFile helper. Then you can use this code.
Recipes Template
<template name="recipes">
{{#each showRecipes}}
<div class=" col-md-4">
<div class="panel panel-default mb " >
<div class="panel-image">
{{#with FS.GetFile "recipesImages" image}}
<img src="{{url}}" class="panel-image-preview" />
{{/with}}
</div>
<div class="panel-body">
<h4>{{name}}</h4>
<p>{{description}}</p>
</div>
<div class="panel-footer text-center" >
<span class="glyphicon glyphicon-open-file"></span>
</span><small> {{time}}</small>
<span class="glyphicon glyphicon-heart likes" style="vertical-align:middle"></span><small>2 </small>
<span class="glyphicon glyphicon-share"></span>
</div>
</div>
</div>
{{/each}}
</template>
Recipes helpers
Template.recipes.helpers({
'showRecipes':function(){
return Recipes.find();
}
});
Here's the link to cfs:ui documentation

Bootstrap Twitter Modal - Clearing dialog input fields

I have created a modal dialog using Bootstrap and I'm having a problem with the Modal dialog box not clearing form input fields after the box is closed or after it's submitted.
HTML
<!-- Password Reset Modal -->
<div id="passwModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4>Forgot Password?</h4>
<p>Please enter your registered email address below and a new password will be sent to you.</p>
</div>
<div class="modal-body">
<div id="sent"><!-- Display sent message after password reset -->
<form class="form-horizontal" id="reset">
<div class="control-group">
<label class="control-label" for="Email">Username</label>
<div class="controls">
<input class="input-large" type="email" id="Username" name="Username" placeholder="Email Address">
</div>
</div>
<div class="controls">
<button class="btn btn-inverse" id="close" value="CANCEL" data-dismiss="modal" aria-hidden="true">CLOSE</button>
<button class="btn btn-success" id="submit">SEND</button>
</div>
</form>
</div><!-- end of sent -->
</div>
</div><!-- end of Password Reset Modal -->
JavaScript (Validation & ajax)
$(document).ready(function () {
jQuery.validator.addMethod("accept", function (value, element, param) {
return value.match(new RegExp("." + param + "$")); });
$("#register").validate({
rules: {
FirstName: {
required: true,
accept: "[a-zA-Z]+"
},
LastName: {
required: true,
accept: "[a-zA-Z]+"
},
Pwd: {
required: true,
minlength: 8
},
CPwd: {
required: true,
minlength: 8,
equalTo: "#Pwd"
},
Email: {
required: true,
email: true
},
Agree: "required"
},
messages: {
FirstName: {
required: "First name required.",
accept: "Letters only please."
},
LastName: {
required: "Last name required.",
accept: "Letters only please."
},
Pwd: {
required: "Please create a password.",
minlength: "Password must be at least 8 characters."
},
CPwd: {
required: "Please confirm password.",
minlength: "Password must be at least 8 characters.",
equalTo: "Passwords do not match."
},
Email: "Email address is not valid.",
Agree: "Please accept our policy."
},
submitHandler: function() {
$.ajax({
type: "POST",
url: "../process.php",
data: $('form#register').serialize(),
success: function (msg) {
$("#thanks").html(msg)
},
error: function () {
alert("failure");
}
});
}
});
}); // end document.ready
Just an update to the previous answer for anyone now using the new Bootstrap, currently v3.1.1, that needs this answered!
$('#passwModal').on('hidden.bs.modal', function (e) {
$('#Username').val('');
});
To ensure that your form will be empty when you will reopen the modal, you can do this for every field on your form. It will delete the last value of your field on the hidden state of the modal when this one is closing :
$('#passwModal').on('hidden', function () {
$('#Username').val("");
});

codeigniter datatables get data from form

Pls help basically I want to filter my datatable using form.
each input has a value that would affect the query with submitted form.
here is my JQUERY.
$("#submit").click(function (e) {
$('#table').dataTable
({
"sAjaxSource": "index.php/report/get_report",
"sServerMethod": "POST",
'fnServerData': function (url, data, callback) {
// Add new data
dataString = $("#myform").serialize();
$.ajax({
'url': "index.php/report/get_report",
'data': dataString,
'type': 'POST',
'success': callback,
'dataType': 'json',
'cache': true
});
},
'bServerSide' : true,
"aaSorting": [[ 3, "desc" ]],
"bPaginate": true,
"bSortClasses": false,
"bAutoWidth": true,
"bInfo": true,
"iDisplayLength" : 3,
"bScrollCollapse": true,
"oLanguage": {
"sSearch": "Search:"
},
"bDestroy": true
});
});
and this is my HTML FORM
<form name="myform">
<label>Employee:</label>
<input type="text" name="employeeid" id="employeeid" title="Type Employee" />
<label>Training Type: </label>
<select name="trainingtype" id="trainingtype" >
<option value="" selected="selected">All</option>
<option value="1">Externally Facilitated Training</option>
<option value="3">Internally Facilitated Training</option>
<option value="2">Webcast/E-Learning</option>
</select>
<label>Datestart</label>
<input type="text" class="field size3" name="datestart" id="datepicker_s" />
<label>Dateend </label>
<input type="text" class="field size3" name="dateend" id="datepicker_e" />
<input type="hidden" id="txtsearchid" name="txtsearchid">
<input type="button" class="button" value="Submit" id="submit" />
when I submit my form I get nothing.
Im I doing the right way?
pls help.
GOT IT
"fnServerData": function ( sSource, aoData, fnCallback ) {
//REQUIRED: Add a Post variable with the object value
aoData.push(
{ "name": "txtsearchid", "value": $( "#txtsearchid" ).val() },
{ "name": "datestart", "value": $( "#datepicker_s" ).val() },
{ "name": "dateend", "value": $( "#datepicker_e" ).val() },
{ "name": "trainingtype", "value": $( "#trainingtype" ).val() }
);
$.ajax( {
dataType: 'json',
type: "POST",
url: sSource,
data: aoData ,
success: fnCallback
} );
},
this must be the solution for my problem. I didnt used the serialized instead of push
Use Firebug add ons from Mozilla to check the error