I've assembled a page as a proof-of-concept for retrieving matching user names from Active Directory (via a web service on out intranet) while typing values into a text box. It works beautifully, but only on my workstation and only in IE (version 10). Every other user who has tried it gets the alert, "error", which is also what I get when I try it in Chrome. I've further verified that the other users can actually retrieve the data by pasting the web service URL directly into their browsers, so the breakdown occurs somewhere between fetching the data and displaying in the form. My only thought was that maybe there are some browser settings responsible for allowing this to work, but I really have no idea what those might be.
Here's what the code looks like:
<div>
<h1>Active Directory lookup</h1>
<input name="txtADAccount" type="text" id="txtADAccount" style="width:400px;" />
</div>
</form>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
$("#txtADAccount").autocomplete({
source: function (request, response) {
$.support.cors = true;
$.ajax({
crossDomain: true,
headers: { 'Access-Control-Allow-Origin': '*' },
url: "http://fetch.mycompany.com/ADUser/ADUserByAccountOrDisplayName/" + $('#txtADAccount').val(),
dataType: "json",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
var jsonData = JSON.parse(data);
// Filter out records where there is no email address
var jsonFiltered = jsonData.result.filter(function (row) {
if(row.mail != '') {
return true
} else {
return false;
}
});
response($.map(jsonFiltered, function (item) {
return { label: item.displayName + ' (' + item.mail + ')' }
}))
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus + ': ' + errorThrown);
}
});
},
select: function (event, ui) {
// prevent autocomplete from updating the textbox
event.preventDefault();
$(this).val(ui.item.label);
},
minLength: 2 //minLength as 2, it means when ever user enter 2 character in TextBox the AutoComplete method will fire and get its source data.
});
});
</script>
</body>
Just looking for some hints as to what the source of the problem might be.
UPDATE: Updating the script links to the latest versions of jquery and jquery.ui resulted in it working on at least one more workstation that didn't work previously. However, it's still not working on any other machines I've tried and the error messages displayed are not consistent. Sometimes it just displays "error", other times "error: No Transport", and other times "error: Access is Denied".
UPDATE 2: On this second workstation where I've been able to get it working, I noticed that if I log in using a different account, I get the "error: Access is denied" message. This is puzzling... to what exactly is access being denied? It's not the web service itself because any user can drop the web service URL into their browser and it will return the appropriate json data.
UPDATE 3: I just discovered that changing the contentType to "application/x-www-form-urlencoded; charset=utf-8" has resulted in it working now for several other users as well as working for me now in Chrome. It's still not working for at least two users, one using IE who gets no message whatsoever, and another who is still getting the "Access is denied" message (I may need to have them try clearing their browser cache). I'm still baffled as to why it always worked for me alone regardless of the contentType setting.
Related
i wanna have a Faceook-Feature in my website: Everytime a user gets a Private Message, the title should be change in something like "[1 PM] TITLE",
How to do that?
I know its easy to change the page-title, the question is how to run a database query every 10 seconds (or is that to often?) and change the title - Without an user interaction (ajax?)
You can use jquery setInterval to make a AJAX request after a certain interval to get data from the db.
$(function(){
setInterval(function() {
$.ajax({
url: "page.html",
success: {
document.title ='new title';
},
error: function(){
//error occurred
}
});
}, 2000);
});
And on AJAX success you can change the page title.
I am working on asp.net website in which i am getting data by using jquery ajax.
here is my code.
function PostSubChapter(qbt_id) {
debugger;
var v1 = 'qbt_id:' + qbt_id;
$.ajax(
{
type: "POST",
url: '<%= ResolveUrl("~/QuestionBankSubChapters.aspx/GetChapters") %>',
data: '{' + v1 + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result.status === "OK") {
alert('Comment posted');
}
else {
fnDisplaySubChapter(result, qbt_id);
}
},
error: function (req, status, error) {
alert("Sorry! Post failed due to error");
}
});
}
and my web method is
[WebMethod]
public static List<BO.QuestionBankSubChapters> GetChapters(int qbt_id)
{
BAL.QuestionBankSubChapters oQBTSC = new BAL.QuestionBankSubChapters();
List<BO.QuestionBankSubChapters> lstQBTSC = oQBTSC.getQuestionBankSubChapters(qbt_id);
return lstQBTSC;
}
These is working very fine in chrome an IE but not showing any result in case of FireFox
call is going properly to web method but at the time of getting result from it is calling Error function.It has to call another function present in sucess function but not calling that function.
Pls help me these.
for your understanding here i am mentioning the link of the website
Link is : "http://skillgun.com/Home.aspx"
Open these in Fire fox and chrome both then Click on Arithmetic then it will display 2nd screen.
Just see the o/p in both the browser you'll understand. In chrome its working properly but in Firefox its not.I am not getting understand what is the reason behind these.Sample code already i mentioned above.
I am using jquery-1.8.3.min.js for getting the result.The callback function is working fine in chrome and IE but not working in FF
Please help me....
Your page method returns correct response, problem is with your other code that you've not posted but I could see in FF console.
You've used innerText at many places and FF doesn't support it. Try innerHTML instead.
For waitprocess div you're not using # along with it's id while using Jquery Selector $ and hence it is visible all the time.
I am trying to pass extra parameters through to my ashx when using Jquery UI auto complete.
I have seen lots of examples and have spent ages fiddling but I can't get mine to work.
I have two auto complete textboxes, what ever is entered in the first one needs to narrow down the search for the second one.
On my first autocomplete textbox all works fine and the ID that is returned is set to a hidden field.
$("#<%=txtSearch1.ClientID%>").autocomplete('/Search1.ashx');
$('#<%=txtSearch1.ClientID%>').result(function (event, data, formatted)
{
if (data) {
var id = data[1];
$("#<%=hdnSearched1.ClientID%>").val(id);
}
});
I want to use the value of the hidden field along with the value the user enters into the second textbox to do the search for the second auto complete
I first tried this:
$("#<%=txtSearch2.ClientID%>").autocomplete('/Search2.ashx',{
extraParams: { "Id": $("#<%=hdnSearched1.ClientID%>").val() }});
This fired my ashx but the Id in the extraparams was blank. A bit of googling told me this was because it was set when the page loaded so the value for the extra param was set before I set the value of the hidden field.
I did a bit more fiddling and searching and came up with something like this which a lot of people seem to be using.
$('#<%=this.txtSearch2.ClientID %>').autocomplete({
source: function (request, response)
{
$.ajax({
url: '/Search2.ashx',
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
data: {
term: request.term,
sId: $("#<%=hdnSearched1.ClientID%>").val()
},
success: function (data)
{
response(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert(textStatus);
}
});
}
});
The problem is this doesn't even fire the ashx! I have been fiddling about with this for ages, following example after example but I can't seem to work what I am doing wrong!
I'm sure it must be obvious!
Can anyone help?
I have an asp mvc 3 application, and there is a view that makes an ajax call, when I run it in visualstudio it workes but when i run it in IIS 7 it is not sending it to the server! I searched for a solution and it said that the urls had to be modified, so i changed it like this using url action but it still doesn't do anything, does anybody know why this might be?
In the webpage I don't see anymessage it simply doesn't do anything.
The ajax funciton is inside the code of the view, it is embedded there, it looks like:
<script type="text/javascript">
function display(Txt) {
$.ajax({
type: "POST",
//url: "/Controller/Action",
url: '#Url.Action("Controller", "Action")',
data: "Id=" + Txt,
success: function (result) {
if (result.Info != undefined) {
//do something
}
else if (result.Info == undefined) {
//do something
}
}
});
}
</script>
The problem was that Url.Action was the other way around:
before:
url: '#Url.Action("Controller", "Action")',
after:
url: '#Url.Action("Action", "Controller")',
This is strange because I checked a blog from microsoft and they had it in the first order =S
First try getting to the Ajax uri in your browser.
If you cannot you may just have set the app up in a different folder structure.
If your controller method has an Ajax attribute remove it for this test.
The answer may be apparent after trying the url (uri)
I'm trying to call a server side method, using jquery, on the textchange event of a textbox which is generated dynamically on the clientside (I dont know how to fetch the id of this). Can somebody help me to do this stuff? The script im using is as follows:
<script type="text/javascript">
$(init);
function init() {
$('#test').droppable( //Div Control where i'll be dropping items
{
drop: handleDropEvent
});
$('a').each(function(idx, item) {
$(item).draggable({ cursor: 'move', helper: 'clone' })
});
}
function handleDropEvent(event, ui) {
var draggable = ui.draggable;
document.getElementById('test').innerHTML += addColumn(draggable.attr('text')) + '<br>';
}
$('.textChangeClass').live('change', function() {
/* Evokes on the text change event for the entire textboxes of class .textChangeClass. Is it possible to specify the dynamic textbox generated # clientside here? (like for e.g. : $('#mytextbox').click(function () ) */
$.ajax({
type: "POST",
url: "Webtop.aspx/ServerSideMethod", //This is not getting called at all.
data: "{'param1': AssignedToID}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg) {
alert("From Server");
}
})
});
});
function addColumn(column) {
var iHtml;
//This is how i'm generating the textboxes along with a checkbox bound by a div.
iHtml = '<div id="dv' + column + '" width="100px;" height="20px;" padding: "0.5em;"> ' + '<span title="ToolTipText">' + '<input type="checkbox" id="cb' + column + '" value="' + column + '" /> <label for="cb' + column + '">' + column + '</label></span><input class="textChangeClass" type="text" id="aln' + column + '"> </div>';
return iHtml
}
</script>
I think you have an extra "});" although this probably isn't the problem.
What is "AssignedToID"? Try adding single quotes around that. I seem to remember having a weird problem a couple years ago related to quoting in the json.
Can you see the request in Fiddler/firebug/etc? Is the content correct?
You should be careful of your use of inferred semi-colons too. If you ever minify your javascript (yeah, I know this is embedded, but I'd like to hope that one day it will be moved to a seperate js file) you're eventually going to have a problem. Imagine some other developer comes along, does some refactoring and needs to add a return value after the ajax call.
$.ajax({...})return foo}
EDIT
Fiddler/Firebug Net panel are your friends... They will allow you to inspect the request and the response from the server. This way you don't have to add the error handler (although you may want to for other reasons eventually)
EDIT
To answer the other part of your question, you can access the textbox for which the change event was triggered through the use of the 'this' keyword inside of the event handler.
$('.textChangeClass').live('change', function(event) {
//Note that the 'event' parameter has interesting things in it too.
var changedText = $(this).val();
alert("The value in the textbox is: '" + changedText + "'");
var data = {
param1: changedText
};
$.ajax({
...
//Using json2 library here to create json string
data: JSON.stringify(data),
...
});
});
Note that I added the optional 'event' parameter to the event handler. It has interesting things in it and it's something that is often overlooked by people who are new to jQuery. Read about it here.
You need to write you code of adding the event to textbox after generation of textbox otherwise it's not get fire.
add text box
After that write code to add event to text box or bind event to text box
just follow the above step will do your work
EDIT
Add the error function to your ajax call you will get the error ... will allow you to proceed further
$.ajax({
type: "post", url: "/SomeController/SomeAction",
success: function (data, text) {
//...
},
error: function (request, status, error) {
alert(request.responseText);
}
});