I'll start from the beginning. I'm building a wordpress plugin that does double duty, in that it can be inserted in to a post via a shortcode, or added as a sidebar widget. All it does is output some js to make jquery.post requests to a local php file. The local php file makes a request to a webservice for some data. (I had to do it this way instead of directly querying the web service with jquery.ajax because the url contains a license key that would be public if put in the js).
我会从头开始。我正在构建一个双重任务的wordpress插件,因为它可以通过短代码插入到帖子中,或者添加为侧边栏小部件。它只是输出一些js来使jquery.post请求到本地php文件。本地php文件向web服务请求某些数据。 (我必须这样做,而不是直接用jquery.ajax查询Web服务,因为url包含一个许可密钥,如果放入js将是公共的)。
Anyway, When I am viewing a page in the wordpress blog that has both the sidebar widget and the plugin output via shortcode only one of the requests work. I mean it works in that it gets a response back from the php script. Once the page is loaded they both work normally when manually told to.
无论如何,当我在wordpress博客中查看具有侧边栏小部件和通过短代码插件输出的页面时,只有其中一个请求有效。我的意思是它的工作原理是它从php脚本获得响应。页面加载后,当手动告知时,它们都能正常工作。
Webpage view -> send 2 post requests to my php script -> both elements should be filed in, but only one is.
网页视图 - >发送2个帖子请求到我的PHP脚本 - >两个元素都应该归档,但只有一个是。
My php script is just:
我的PHP脚本只是:
<?php
if(isset($_POST["zip"])) {
// build a curl object, execute the request,
// and basically just echo what the curl request returns.
}
?>
Pretty basic.
here is some js some people wanted to see:
这里有一些人想看到的js:
function widget_getActivities( zip ){
jQuery("#widget_active_list").text("");
jQuery.post("http://localhost/wordpress/wp-content/ActiveAjax.php", { zip: zip},
function(text) {
jQuery(text).find("asset").each(function(j, aval){
var html = "";
html += "<a href='" + jQuery(aval).find("trackback").text() + "' target='new'> " + jQuery(aval).find("assetName").text() + "</a><b> at </b>";
jQuery("location", aval).each(function(i, val){
html += jQuery("locationName", val).text() + " <b> on </b>";
});
jQuery("date", aval).each(function(){
html += jQuery("startDate", aval).text();
<!--jQuery("#widget_active_list").append("<div id='ActivityEntry'>" + html + " </div>");-->
jQuery("#widget_active_list")
.append(jQuery("<div>")
.addClass("widget_ActivityEntry")
.html(html)
.bind("mouseenter", function(){
jQuery(this).animate({ fontSize: "20px", lineHeight: "1.2em" }, 50);
})
.bind("mouseleave", function(){
jQuery(this).animate({ fontSize: "10px", lineHeight: "1.2em" }, 50);
})
);
});
});
});
}
Now imagine there is another function identical to this one except everything that is prepended with 'widget_' isn't prepended. These two functions get called separately via:
现在假设有另一个与此相同的功能,除了前面带有'widget_'的所有内容都没有预先添加。这两个函数通过以下方式单独调用:
jQuery(document).ready(function(){
w_zip = jQuery("#widget_zip").val();
widget_getActivities( w_zip );
jQuery("#widget_updateZipLink").click(function() { //start function when any update link is clicked
widget_c_zip = jQuery("#widget_zip").val();
if (undefined == widget_c_zip || widget_c_zip == "" || widget_c_zip.length != 5)
jQuery("#widget_zipError").text("Bad zip code");
else
widget_getActivities( widget_c_zip );
});
})
I can see in my apache logs that both requests are being made. I'm guessing it is some sort of race condition but that doesn't make ANY sense. I'm new to all this, any ideas?
我可以在我的apache日志中看到两个请求都在进行中。我猜这是某种竞争条件,但这没有任何意义。我对这一切都是新手,有什么想法吗?
EDIT: I've come up with a sub-optimal solution. I have my widget detect if the plugin is also being used on the page, and if so it waits for 3 seconds before performing the request. But I have a feeling this same thing is going to happen if multiple clients perform a page request at the same time that triggers one of the requests to my php script, because I believe the problem is in the php script, which is scary.
编辑:我想出了一个次优解决方案。如果插件也在页面上使用,我有我的小部件检测,如果是,它在执行请求之前等待3秒。但我有一种感觉,如果多个客户端同时执行一个页面请求会触发我的php脚本的一个请求,那将会发生同样的事情,因为我认为问题出在php脚本中,这很可怕。
1 个解决方案
#1
Not really enough information for me to really figure anything out...
对我来说真的没有足够的信息可以解决任何问题......
However one thing to try is to get firebug and see what the response is for each of the requests.
然而,要尝试的一件事是获取firebug并查看每个请求的响应。
Another question might be...are you filling in by id? If so, then it may be due to the fact that you have the same id twice in one page, and that is messing things up. If you have it online it would be much easier to help you out.
另一个问题可能是......你在用id填写吗?如果是这样,那么可能是因为你在一个页面中有两次相同的id,这就是弄乱了。如果你有在线帮助你会更容易。
Edit:
A few comments...I'm not sure if anything will be solved by this...however...
一些评论......我不确定这有什么事情可以解决......但是......
<!--jQuery("#widget_active_list").append("<div id='ActivityEntry'>" + html + " </div>");-->
As far as I know the type of comment you use there is only effective when used in html and might be screwing up your Javascript. Replace it with //
据我所知,你使用的评论类型只有在html中使用时才有效,可能会搞砸你的Javascript。替换为//
jQuery("#widget_active_list").append(jQuery("<div>")
.addClass("widget_ActivityEntry")
.html(html)
.bind("mouseenter", function(){
jQuery(this).animate({ fontSize: "20px", lineHeight: "1.2em" }, 50);
})
.bind("mouseleave", function(){
jQuery(this).animate({ fontSize: "10px", lineHeight: "1.2em" }, 50);
})
);
Second of all...that whole block is nested in the last .each which doesn't seem to be having an effect however, it is not impossible that it messing with it.
第二个......整个块都嵌套在最后一个。但是它似乎没有效果,但它不是不可能搞乱它。
Last of all, while it might not actually be making a difference, you should probably be setting the type of the response to xml. Using both the http headers in php and using the last argument of the function. i.e.
最后,虽然它实际上可能没有什么区别,但您可能应该将响应的类型设置为xml。在php中使用http标头并使用函数的最后一个参数。即
header("Content-Type: application/xml");
and
jQuery.post("http://localhost/wordpress/wp-content/ActiveAjax.php", { zip: zip}, thefunctionhere, xml);
If this doesn't help, my suggestion for a sub-optimal solution, is to use a get request instead, and add a random parameter that isn't taken into account to both requests, however make it the same for each request, but different every time you go to the page. That way the browser will cache the request for the first one, and not query the server a second time, but it won't cache it for when the person comes to the page again.
如果这没有帮助,我对次优解决方案的建议是改为使用get请求,并添加一个未考虑到两个请求的随机参数,但是对于每个请求都要相同,但是每次你去页面都不一样。这样浏览器将缓存第一个请求,而不是第二次查询服务器,但是当用户再次访问页面时,它不会缓存它。
#1
Not really enough information for me to really figure anything out...
对我来说真的没有足够的信息可以解决任何问题......
However one thing to try is to get firebug and see what the response is for each of the requests.
然而,要尝试的一件事是获取firebug并查看每个请求的响应。
Another question might be...are you filling in by id? If so, then it may be due to the fact that you have the same id twice in one page, and that is messing things up. If you have it online it would be much easier to help you out.
另一个问题可能是......你在用id填写吗?如果是这样,那么可能是因为你在一个页面中有两次相同的id,这就是弄乱了。如果你有在线帮助你会更容易。
Edit:
A few comments...I'm not sure if anything will be solved by this...however...
一些评论......我不确定这有什么事情可以解决......但是......
<!--jQuery("#widget_active_list").append("<div id='ActivityEntry'>" + html + " </div>");-->
As far as I know the type of comment you use there is only effective when used in html and might be screwing up your Javascript. Replace it with //
据我所知,你使用的评论类型只有在html中使用时才有效,可能会搞砸你的Javascript。替换为//
jQuery("#widget_active_list").append(jQuery("<div>")
.addClass("widget_ActivityEntry")
.html(html)
.bind("mouseenter", function(){
jQuery(this).animate({ fontSize: "20px", lineHeight: "1.2em" }, 50);
})
.bind("mouseleave", function(){
jQuery(this).animate({ fontSize: "10px", lineHeight: "1.2em" }, 50);
})
);
Second of all...that whole block is nested in the last .each which doesn't seem to be having an effect however, it is not impossible that it messing with it.
第二个......整个块都嵌套在最后一个。但是它似乎没有效果,但它不是不可能搞乱它。
Last of all, while it might not actually be making a difference, you should probably be setting the type of the response to xml. Using both the http headers in php and using the last argument of the function. i.e.
最后,虽然它实际上可能没有什么区别,但您可能应该将响应的类型设置为xml。在php中使用http标头并使用函数的最后一个参数。即
header("Content-Type: application/xml");
and
jQuery.post("http://localhost/wordpress/wp-content/ActiveAjax.php", { zip: zip}, thefunctionhere, xml);
If this doesn't help, my suggestion for a sub-optimal solution, is to use a get request instead, and add a random parameter that isn't taken into account to both requests, however make it the same for each request, but different every time you go to the page. That way the browser will cache the request for the first one, and not query the server a second time, but it won't cache it for when the person comes to the page again.
如果这没有帮助,我对次优解决方案的建议是改为使用get请求,并添加一个未考虑到两个请求的随机参数,但是对于每个请求都要相同,但是每次你去页面都不一样。这样浏览器将缓存第一个请求,而不是第二次查询服务器,但是当用户再次访问页面时,它不会缓存它。