I am using jQuerys autocomplete. When page is load below ajax run automaticly. But i want to do this if anyone keydown this textbox. How can i change ?
$('#txtFirmaAdlari').autocomplete({
source: function (request, response) {
$.ajax({
url: '/Fatura/GetFirma/',
dataType: "json",
cache: false,
traditional: true,
Related
I'm working with two individual mvc application like A and B. I'm running these two application on localhost(IIS). On application A i have an api controller called student. Now i want these student data through student api and show on application B. How can i retrieve those data through this student api?
i try this ajax request from application B,
$.ajax({
url: 'http://localhost:123/api/StudentApi',
type: "GET",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
I've deployed my MVC project application to IIS 7, and it seems to work fine till I execute an ajax request with the following format:
$.ajax({
type: 'POST',
url: "/ControllerName/ActionMethodName",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: dataToSend,
success: function (data) {
//Do Something
}
});
Then It eliminates the application name assigned by IIS and directly access the contoller/actionMethod path from the host!
Can anyone help me solve this?
Thanks
I am trying to upload file with jqgrid, but the is some mistake in my code.
I am using onInitializeForm method.
I am using Jquery Ajax File Uploader
Can anyone do help to me?
You can see Error in screenshot
This is my code.
jQuery("#formGrid").jqGrid(
{
url:'application/ajax/common_form_detail.php?form_id='+form_id,
editurl:'application/ajax/common_form_edit.php?form_id='+form_id,
datatype: "json",
colNames:[<?php echo $strfield; ?>],
colModel:[<?php echo $strFieldModel; ?>],
rowNum:20,
rowList:[20,50,100],
pager: '#formControl',
sortname: 'id',
viewrecords: true,
sortorder: "asc",
autowidth: true,
height: 400,
width: 900
});
jQuery("#formGrid").jqGrid('navGrid','#formControl',
{ edit:true,add:true,del:true,search:false},
{width:780,recreateForm:true},
{width:780,recreateForm:true
,onInitializeForm : function(formid){
$(formid).attr('method','POST');
$(formid).attr('action','');
$(formid).attr('enctype','multipart/form-data');
}, afterSubmit : function(response, postdata){
$.ajaxFileUpload({
url: 'application/ajax/common_file_upload.php',
secureuri:false,
fileElementId:'STUDENT_AVATAR',
dataType: 'json',
success: function (data, status) {
alert("Upload Complete.");
}
});
}
});
});
One can see that you have error in jQuery.handleError function which is depreciated from jQuery 1.5. jQuery Ajax File Uploader V2.1 provided from the download page included jQuery 1.2.1.
One have to fix the code of Ajax File Uploader to solve the problem. See the answer and the answer for example or this one. You can consider to use another Plugin for file upload like jQuery Form Plugin (and here), see here.
I using this script to upload file (one by one) with HTML5 FormData in Rails 3.2.8 application.
http://jsfiddle.net/RamPr/
$('.uploader input:file').on('change', function() {
$this = $(this);
$('.alert').remove();
$.each($this[0].files, function(key, file) {
$('.files').append('<li>' + file.name + '</li>');
data = new FormData();
data.append(file.name, file);
$.ajax({
url: $('.uploader').attr('action'),
contentType: 'multipart/form-data',
type: 'POST',
dataType: 'json',
data: data,
processData: false
});
});
});
But when I upload a file, I get this error in console:
webrick/server.rb:191:in `block in start_thread' ERROR ArgumentError: invalid %-encoding ("filename.jpeg" Content-Type: image/jpeg
How can I solve this error?
Have you seen this issue? Sending multipart/formdata with jQuery.ajax
It looks like you might be running into jQuery adding content-type headers, which causes the boundary string to be missing. From the above linked issue:
It’s imperative that you set the contentType option to false, forcing jQuery not to add a Content-Type header for you, otherwise, the boundary string will be missing from it. Also, you must leave the processData flag set to false, otherwise, jQuery will try to convert your FormData into a string, which will fail.
Based on that, give this a try:
$.ajax({
url: $('.uploader').attr('action'),
contentType: false,
cache: false,
processData: false,
type: 'POST',
dataType: 'json',
data: data
});
I haven't tried this myself, but I suspect this might be the droids you're looking for :)
In my controller I have
public JsonResult GetInfo(string id)
in my js
$.ajax({
contentType: 'application/json, charset=utf-8',
type: "POST",
url: "/Incidents/GetInfo",
data: { id: "777" },
cache: false,
dataType: "json",
success: function (response) {
//etc....
jquery ajax error delegate gets executed. If I use
data: { "777" },
no error, but the value doesn't get passed. This should be easy but I am beating my head against the wall. Maybe I am not allowed to pass strings to controller's actions?
What am I doing wrong here?
You are indicating application/json request and you are sending a application/x-www-form-urlencoded request. So you will have to choose between one of the two ways to encode parameters and not mix them.
application/x-www-form-urlencoded:
$.ajax({
type: "POST",
url: "/Incidents/GetInfo",
data: { id: "777" },
cache: false,
dataType: "json",
...
});
application/json:
$.ajax({
type: "POST",
url: "/Incidents/GetInfo",
contentType: 'application/json, charset=utf-8',
data: JSON.stringify({ id: "777" }),
cache: false,
dataType: "json",
...
});
The JSON.stringify method is natively built into modern browsers and is used to convert the javascript literal into a JSON string which is what we indicated that we are going to send the request as. If you are having to support legacy browsers you could include the json2.js script to your page which contains this method.
As a side note the dataType: "json" setting is not needed since the server will set the proper Content-Type header to application/json and jQuery is smart enough to use that.
And as a second side note you really don't want to be hardcoding an url like this in your javascript file: url: "/Incidents/GetInfo". What you want is to use url helpers when generating urls: url: "#Url.Action("GetInfo", "Incidents")".
Are you missing HttpPost attribute in your action? If not, use something like firebug or chrome dev tools to see http request/response and get more details...
[HttpPost]
public JsonResult GetInfo(string id)