I'm unsure if I'm just thinking of this the wrong way of thinking or I'm just missing something.
I have a search box that does some wildcard searches in a database. I have made this an ajax request so I don't need to reload the page and I can have a little please wait icon while the data loads.
Now as there is pagination so I could do with the url parameters being added to the url, can you do this on a ajax request, ad if so how?
here is a sample code (param1) :
data : $('#form-contactSupplementaire').serialize() + "¶m1=value",
Request full ajax :
$.ajax({
method : "POST",
url : "...",
data : $("...").serialize() + "¶m1=value",
dataType: "",
success : function (json) {
// ...
console.log(json);
}, error: function () {
console.log("Error");
}
});
Related
Trying to override the Apify's Google Scraper actor's queries by passing the data object as given below. I am getting 400 and and 403 error message. When I remove the data playload, it works fine. It then returns the result with the default queries.
1) What is the right way to pass the playload to override the queries parameters.
2) How can I send multiple search queries like "link building", "link building service" ?
$.ajax({
url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120,
method : 'POST',
contentType: 'application/json',
data : { // when I add this I get 400 error.
"queries" : "Outreach link building",
},
success:function(result) {
console.log(result);
}
});
Thanks in advance.
1) You need to stringified JSON and use proper dataType:
$.ajax({
url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120',
method : 'POST',
contentType: 'application/json',
dataType: 'json',
data : JSON.stringify ({
"queries" : "Outreach link building"
}),
success:function(result) {
console.log(result);
}
});
You can read about that in this post.
2) If you want to send multiple queries you need to separate them using the new line:
{
"queries": "Outreach link building\nquery"
}
I'm making an ajax post call on click of a button as below. But on success, the page is again submitted to the form-post url on the jsp page. I'm not sure why this is happening and how I can prevent the same.
Ajax call:
var urlsub = window.location.protocol + "//"+ window.location.host + contextPath+ "/saveEOI";
$.ajax({
type : "POST",
url : urlsub,
cache : false,
async: false,
data: {'applicationId' : applicationId
},
success : function(data) {
alert("success");
},
error : function(e) {
console.log(e);
}
});
The success alert is successfully shown, but after that another post request is generated for the jsp page form action...which causes a 405 error. I want to stop the execution after success.
<s:form action="expressEOI" class="form-horizontal" name= "EOI" id="EOI"
enctype="multipart/form-data" modelAttribute="expressEOIBean">
Request if anybody can help me out..where I am going wrong.
I am running an ajax call to a servlet on click of a link. The link does not have the servlet called directly. Its called from an 'onClick' function instead.
$('#exportExcelLink').on("click", function(e) {
e.preventDefault();
var docStatus=JSON.stringify(g_checkboxVal);
$.ajax({
type : "GET",
url : '/bin/servlets/dashboardexcelexport',
data : { docStatus: docStatus },
contentType: 'application/json',
success : function(data) {
console.log("excel export call success");
},
error : function(data) {
console.log("error occured in excel export call");
}
});
});
I can see the excel being generated in my server logs and the response headers also show the file name and type (seen in network tab of console).
But the file does not pop up for me to open or save. There is no activity on the page at all.
Do I need to do something else ?
Use the built-in createObjectURL method to create a URL with your Ajax response. Then create a hidden link and make the href the object you created. Then use JavaScript to click the link
I want to send data from HTML view to Django backend to process and then the backend will send an url back the to HTML view which will redirect the user to another page. It's like this:
Page 1 ----user's input data --- Ajax --- > backend --- process --- a url to page 2 --- Ajax --> page 2
The problem is that, I don't know how to send the URL back Ajax after processing user's data, so I have to redirect by using window.location.href = '/' . But I think the code is not clean this way. I wonder if there is a way to send url to Ajax's success from backend.
Here is the code n the HTML :
function doSomething(data){
$.ajax({
type: "POST",
url: URL_POST_BY_AJAX,
data: {
'data' : data,
},
success: function (response) {
window.location.href = '/';
},
error: function (data) {
alert('failed');
}
});}
Please help me. Thank you!
In your processing view:
from django.http.response import JsonResponse
def whatever_your_view_name_is(request):
(data processing...)
return JsonResponse({'url': the_url_to_page_2)
then in your JS:
(...)
success: function (response) {
window.location.href = response.url;
},
(...)
Mostly, rest api url' s like:
/api/student:id/notes
I create a rest api and my urls like above. But I dont know how to add parameter before notes.
$.ajax({
url : "api/v1/student/notes",
data : { "id" : uid },
type : "get",
headers : { "Authorization" : api_key },
dataType : "json",
success : function(response) {
console.log(response);
}
});
When I call like this, the url seems /api/student/notes?id=5. How can Iadd id parameter between student and notes?
Example url: http://www.sitepoint.com/best-practices-rest-api-scratch-introduction/
AFAIK, jQuery does not offer a neat API for injecting resource URI fragments like Angular's $resource. You'll have to build the URL yourself. Something like
$.ajax({
url: 'api/v1/students/' + encodeURIComponent(uid) + '/notes',
// and so on, no "data" required.