Upload files and Json to server using Flask framework - json

I am using Flask to design a dynamic form that send a json and files to the server with Ajax. For the Json part I succeed with this JQuery code:
$("#insert_recipe").submit(function(){
$.ajax({
type : "POST",
url : "/_insert_recipe",
contentType : 'application/json; charset=utf-8',
data : JSON.stringify(recipe),
dataType: 'json',
success: function(data, textStatus){
window.location.replace("/");
$("#insert_recipe").remove();
}
However, Now I want to add files to the submit function, how to do that?
Thanks

Related

Handlebars escaping '\' character and number followed after it in AJAX request even after using {{{ expression

I am using an AJAX request and Handlebars to communicate the data transfer as shown below.
main.hbs
<script>
..
$.ajax({
type: "POST",
data: JSON.stringify({
propId: "{{staysAt}}",
propImg: "/{{propImg}}",
_csrf: "{{csrfToken}}"
}),
contentType: 'application/json',
dataType: 'json',
url: "/user/sendRequest",
success: function(result){
},
error: function(err){
//console.log(err);
}
})
..
</script>
In propImg data is being stored as
/uploads 8-11-27T10-54-19.118Zparis-graph-colored.png
Instead of
/uploads\2018-11-27T10-54-19.118Zparis-graph-colored.png
When i use main.hbs to display the Handlebar object outside the AJAX request it works perfectly
<h2>/{{propImg}}</h2>
and gives the complete image path, however inside the AJAX call I do not understand what is going wrong.
I tried using {{{propImg}}} for no escape as well however the same issue is coming.

Operation not supported error while PUT to Dynamic CRM Web API

I'm using the following JS to change the name of an account entity using CRM 2016's Web API:
data = JSON.parse('{"name":"<new name>"}');
data = JSON.stringify(data);;
$.ajax({
type: "PUT",
url: "https://<mySite>.dynamics.com/api/data/v8.0/accounts(<accountId>)",
data: data,
contentType: "application/json"
});
But my site returns the following error:
Message":"Operation not supported on account","ExceptionMessage":"Operation not supported on account","ExceptionType":"Microsoft.Crm.CrmHttpException"
What might be the problem?
When using PUT request to update a single property, the property name should be appended to the Uri of the entity.
Try this script:
data = JSON.parse('{"value":"<new name>"}');
data = JSON.stringify(data);;
$.ajax({
type: "PUT",
url: "https://<mySite>.dynamics.com/api/data/v8.0/accounts(<accountId>)/name",
data: data,
contentType: "application/json"
});

How to call .asmx web service using plain html page

I am trying to call an asmx web service in plain html page. But not getting output. I'm not getting the exact problem. Here is my ajax code that I wrote to call that asmx web service.
function Getdet(Name)
{
alert('hellotest');
$.ajax({
type: "POST",
url: "http://192.168.1.20/myservice/service.asmx?HelloWorld", // add web service Name and web service Method Name
data: "{''}", //web Service method Parameter Name and ,user Input value which in Name Variable.
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response)
{
alert('abcd1');
$("#spnGetdet").html(response.d); //getting the Response from JSON
},
failure: function (msg)
{
alert(msg);
}
});
}
Should I have to add anything in the code..?? Please let me know where I am going wrong in this above code.
ASMX pages are plain old Web Services and the output is (almost) always in XML format, so don't except JSON out of the box.
Plus, they don't work well on different domain call by default, you will need to ensure that the cross domain is correctly handled.
After all this is done, you will then use the call as:
success: function(response) {
// http://stackoverflow.com/a/1773571/28004
var xml = parseXml(response),
json = xml2json(xml);
$("#spnGetdet").html(json.d);
}

Getting jQuery.ajax to play nice with my jsonp webservice

I'm having difficulty getting $.ajax to work with my webservice.
I have a webservice I wrote in Java (and I suck at Java) which expects urls like example.com/webservice/somestuff/callback
and returns JSONP like callback({"success":true})
My jquery resembles:
$.ajax({
type: "GET", dataType: "jsonp",
url: '//example.com/someStuff',
}).done(doSomething)
The problem is, all the nice stuff jQuery does to make JSONP work is messing me up. It appends a querystring (i.e. ?callback=jQuery19786...), where my Service expects just a regular path parameter.
If I could somehow convince jQuery to do example.com/webservice/somestuff/jQuery19786... that would be ideal.
Ideas?
Just place ?? where you want the callback to be placed.
$.ajax({
type: "GET", dataType: "jsonp",
url: '//example.com/someStuff/??',
}).done(doSomething)
An alternative is to use the beforeSend option to manipulate the url after jQuery modifies it:
$.ajax({
url: "//example.com/foo/bar/?callback=?",
dataType: "jsonp",
cache: true,
beforeSend: function(jqXHR,options){
options.url = options.url.replace(/\?callback\=/,"");
//console.log(options.url);
//return false;
}
});

how to send GET parameters as JSON using any rest client

I want to send a request with parameters as json. I tried to set these in request body but this doesn't work. Is there any other way to send parameters as JSON?
Thanks,
$.ajax({
type: 'GET',
url: url,
data: JSON.stringify(myJsonObject),
success: successCallback,
dataType: 'json'
});