简体   繁体   English

jquery .get / .post不工作​​ie 7或8,在ff中工作正常

[英]jquery .get/.post not working on ie 7 or 8, works fine in ff

I have basically this on a page: 我基本上在页面上有这个:

<script type="text/javascript">
function refresh_context() {
    $("#ajax-context").html("Searching...");
    $.get("/ajax/ldap_search.php", {cn: $("#username").val()}, function(xml) {
        $("#ajax-context").html($("display", xml).text());
        $("#context").val($("context", xml).text());
    }, 'xml');
}
$(document).ready(function() {
    $("#username").blur(refresh_context);
});
</script>

<input type="text" name="username" id="username" maxlength="255" value="" />
<input type="hidden" name="context" id="context" value=""/>
<div id="ajax-context"></div>

What it should do (and does fine on Firefox) is when you type a username in to the #username field, it will run /ajax/ldap_search.php?cn=$username, which searches our company's ldap for the username and returns it's raw context and a formatted version of the context like this: 它应该做什么(并且在Firefox上运行正常)是当你在#username字段中输入用户名时,它将运行/ajax/ldap_search.php?cn=$username,它会在我们公司的ldap中搜索用户名并返回它原始上下文和上下文的格式化版本,如下所示:

<result>
    <display>Staff -&gt; Accounting -&gt; John Smith</display>
    <context>cn=jsmith,ou=Accounting,ou=Staff,ou=Users,o=MyOrg</context>
</result>

The formatted version (display) goes to the div #ajax-context and goes to the hidden input #context. 格式化版本(显示)转到div#ajax-context并转到隐藏输入#context。 (Also, the -> are actually - "& gt ;" (without spaces)). (另外, - >实际上是 - “&gt;”(没有空格))。

However, on IE the div stays stuck on "Searching..." and the hidden input value stays blank. 但是,在IE上,div仍停留在“正在搜索...”上,隐藏的输入值保持空白。

I've tried both .get and .post and neither work. 我已经尝试了.get和.post,但都没有工作。 I'm sure it's failing on the .get because if I try this, I don't even get the alert: 我确定它在.get上失败了因为如果我试试这个,我甚至都没有收到警报:

$.get("/ajax/ldap_search.php", {cn: $("#username").val()}, function() {
    alert("Check");
});

Also, IE doesn't give me any script errors. 此外,IE不会给我任何脚本错误。

Edit: Added "$(document).ready(function() {", the .blur was already in it in my code, but I forgot to include that in my post. 编辑:添加了“$(document).ready(function(){”,我的代码中已经包含了.blur,但我忘了把它包含在我的帖子中。

Edit 2: The request is being sent and apache2 is receiving it: 编辑2:正在发送请求并且apache2正在接收它:

10.135.128.96 - - [01/May/2009:10:04:27 -0500] "GET /ajax/ldap_search.php?cn=i_typed_this_in_IE HTTP/1.1" 200 69

Problem was in the ldap_search.php file. 问题出在ldap_search.php文件中。 I had this (based on an example I read on someone's blog): 我有这个(基于我在某人的博客上阅读的一个例子):

header("content-type:application/xml-xhtml;charset=utf-8");

It actually needed to be this for IE to read it properly: 它实际上需要这个IE才能正确读取它:

header("content-type:application/xml;charset=utf-8");

God, I hate IE. 上帝,我讨厌IE。

Try changing: 尝试改变:

$("#username").blur(refresh_context);

To: 至:

$(function(){
    $("#username").blur(refresh_context);
});

This will hold off on assigning the blur event until the entire page is loaded. 这将暂停分配blur事件,直到加载整个页面。

Edit: 编辑:

Could it be the use of > in the text of the XML? 可能是在XML文本中使用>吗?

set your type to 'xml' 将您的类型设置为'xml'

jQuery.get( url, [data], [callback], [type] ) jQuery.get(url,[data],[callback],[type])

$.get("/ajax/ldap_search.php", {cn: $("#username").val()}, function() {
    alert("Check");
},'xml');

I had same problem but not with xml - i had simple html as ajax-return. 我有同样的问题,但没有与xml - 我有简单的HTML作为ajax-return。 header("content-type:text;charset=utf-8"); 标题( “内容类型:文本;字符集= UTF-8”); was the solution. 是解决方案。

Does not work: 不起作用:

$.post("/welcome/add_mail", function(data){
       alert('ok');   
});

Works fine with base url in IE7: 在IE7中与基本URL一起正常工作:

$.post("http://localhost/welcome/add_mail", function(data){
       alert('ok');   
});

Can you find out if the Ajax request is even being fired? 你能看出Ajax请求是否被解雇了吗?

You can use Web Development Helper or Fiddler to log Ajax requests. 您可以使用Web Development HelperFiddler来记录Ajax请求。

As general good practice you should enclose any jQuery code that accesses the DOM in a $(document).ready function. 作为一般的良好实践,您应该附上在$(document).ready函数中访问DOM的任何jQuery代码。 This will ensure it doesn't execute until the entire DOM is loaded, although in this instance it doesn't look like that's causing the problem if the div is changing to 'Loading...' 这将确保它在整个DOM加载之前不会执行,尽管在这种情况下,如果div正在更改为“正在加载...”,则看起来不会导致问题。

<script type="text/javascript">
function refresh_context() {
    $("#ajax-context").html("Searching...");
    $.get("/ajax/ldap_search.php", {cn: $("#username").val()}, function(xml) {
        $("#ajax-context").html($("display", xml).text());
        $("#context").val($("context", xml).text());
    }, "xml"); // As pointed out, you should specify the return type
}
$(document).ready(function() {
    $("#username").blur(refresh_context);
});
</script>

You may want to change the $.get call to a $.ajax call, so you can set an error handler to see why it's erroring. 您可能希望将$ .get调用更改为$ .ajax调用,因此您可以设置错误处理程序以查看错误原因。

As I recall, this is done like this: 我记得,这样做是这样的:

$.ajax({
    type: 'GET',
    url: "/ajax/ldap_search.php",
    data: {cn: $("#username").val()},
    success: function(response) { /* do something here */ },
    error: function(xhr, type, exception) { alert("Error: " + type); }
})

The exception object should have more detail about the error as well. 异常对象也应该有关于错误的更多细节。

I had a similar problem, but loading JSON. 我有类似的问题,但加载JSON。 The $.ajax fix worked for me but I also discovered that in my case it had to do with the URL. $ .ajax修复程序对我有用但我也发现在我的情况下它与URL有关。 When I use: 我用的时候:

$.getJSON('',{ ajax: "addressPicker",OID:pickIDNo,s:pickVal}, function(data) {

I would get a silent response, but when I replaced the empty URL '' with '?' 我会得到一个无声的响应,但是当我用'''替换空的URL''时 it worked. 有效。 In your case the URL was present, but it might be picky as to URL. 在您的情况下,URL存在,但它可能是URL的挑剔。

Usually with $.get/$.post you have to specify the return type. 通常使用$ .get / $。post你必须指定返回类型。 This makes it easier for jquery to a) recognize what it looks for and b) decode it for you. 这使得jquery更容易a)识别它的内容并且b)为你解码它。

This may help. 这可能有所帮助。

Instead of $("display", xml).text() 而不是$(“display”,xml).text()

Maybe try: $(xml).find("display").text() 也许试试:$(xml).find(“display”)。text()

Can you see what the response is or is it just timing out? 你能看出它的响应是什么,还是只是超时? Can you see what params are being sent in? 你能看出寄送的是什么吗? If not, expand your: 如果没有,请扩展您的:

function refresh_context() {
    $("#ajax-context").html("Searching...");
    $.get("/ajax/ldap_search.php", {cn: $("#username").val()}, function(xml) {
        $("#ajax-context").html($("display", xml).text());
        $("#context").val($("context", xml).text());
    }, "xml"); // As pointed out, you should specify the return type
}

to

function refresh_context() {
    $("#ajax-context").html("Searching...");
    $.get("/ajax/ldap_search.php", {cn: $("#username").val()}, function(xml) {
        var displayXml = $("display", xml).text();
        $("#ajax-context").html(displayXml);
        var contextXml = $("context", xml).text();
        $("#context").val(contextXml);
    }, "xml"); // As pointed out, you should specify the return type
}

and then debugging the script. 然后调试脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM