javascript jquery .ajax() 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8797169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery .ajax() issue
提问by Johnny
I am using Jquery 1.7.1 and am having issues.. I'm working with a CRM in my script and am working to get the page finished but I'm stuck with this issue..
我正在使用 Jquery 1.7.1 并且遇到了问题。
my html:
我的 HTML:
<form class="collector" action="https://www.domain.biz/admin/transact.php" method="POST">
<input type="hidden" name="method" value="NewProspect">
<input type="hidden" name="campaignId" value="3">
<input type="hidden" name="ipAddress" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
<fieldset>
<div style=" padding-left: 50px">
<table>
<tr>
<td><span style="color:red;">*</span>Your First Name:
<span id="rfvFirstName" style="display:none;">*</span>
</td>
<td><span style="color:red;">*</span>Your Last Name:
<span id="rfvFirstName" style="display:none;">*</span>
</td>
<td><span style="color:red;">*</span>Your Phone Number: </td>
<td><span style="color:red;">*</span>Primary Email: </td>
</tr>
<tr>
<td>
<input name="firstName" type="text" id="firstName" style="width:150px;" value="">
</td>
<td>
<input name="lastName" type="text" id="lastName" style="width:150px;" value="">
</td>
<td>
<input name="phone" type="text" id="phone" class="" style="width:150px;" value="">
</td>
<td>
<input name="email" type="text" id="email" class="required email" style="width:150px;" value="">
</td>
</tr>
</table>
</div>
<div class="clear"></div>
<center>
<input type="submit" name="continue" id="imgbtnSubmit" class="button" style="background-image: url('<?php echo base_url();?>inc/img/button-check.png');
background-repeat: no-repeat; width: 348px; height: 46px; border:none; background-color:transparent;" value="" />
</center>
</fieldset>
<p align="center" style="font-size:12px;">
</p>
</form>
the JS:
JS:
$('.collector').submit(function(){
validate = true;
$(this).find("input:text:visible:enabled").each(function() {
if ($(this).attr("value") == "") {
alert("Please fill in all fields!");
$(this).focus();
validate = false;
return false;
}
else if ($(this).hasClass("email") && !$(this).attr("value").match(/@/)) {
alert("Please enter an email address...");
$(this).focus();
validate = false;
return false;
}
});
if (validate != false) {
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: $(this).serialize(),
success: function(response) {
alert(response);
}
});
}
return false;
});
Now both of these things work, and they work together fine... the issue comes in that I don't get any response and I'm not sure why. I imagine it is because of what firebug is saying... POST https://www.domain.biz/admin/transact.php 200 OK 1.04s jquery.js (line 8102)
现在这两件事都有效,而且它们一起工作得很好……问题在于我没有得到任何回应,我不知道为什么。我想这是因为萤火虫在说什么......POST https://www.domain.biz/admin/transact.php 200 OK 1.04s jquery.js (line 8102)
This line in my firebug is displayed as red, and the line 8102 in jquery.js is: xhr.send( ( s.hasContent && s.data ) || null );
我的firebug中的这一行显示为红色,jquery.js中的8102行是: xhr.send( ( s.hasContent && s.data ) || null );
采纳答案by klvoek
Do you make an cross domain ajax request? I downloaded your code and make a simple test:
你做跨域ajax请求吗?我下载了你的代码并做了一个简单的测试:
Code in
输入代码
- localhost:8080/domain1/a.php
- 本地主机:8080/domain1/a.php
Make a ajax request to
向 ajax 发出请求
- localhost:8080/domain2/b.php
- 本地主机:8080/domain2/b.php
Error happens
发生错误
Code in
输入代码
- localhost:8080/domain1/a.php
- 本地主机:8080/domain1/a.php
Make a ajax request to the page itself
向页面本身发出ajax请求
- (localhost:8080/domain1/a.php)
- (本地主机:8080/domain1/a.php)
No error happens and get the expected response.
不会发生错误并获得预期的响应。
Then I googled the answer for [jquery.ajax cross domain request],and find some links may helps: jQuery AJAX cross domain
然后我在 google 上搜索了 [ jquery.ajax cross domain request]的答案,发现一些链接可能有帮助: jQuery AJAX cross domain
Soluation is : dataType: 'JSONP'
解决方案是:数据类型:'JSONP'
$.ajax({
url:"testserver.php",
dataType: 'JSONP', // Notice! JSONP <-- P
success:function(json){
// do stuff with json (in this case an array)
alert("Success");
},
error:function(){
alert("Error");
},
});
回答by JS Monkey
Here are some suggestions that might help you find the error:
以下是一些可能会帮助您找到错误的建议:
In your ajax call, after the success, add the following code:
在你的ajax调用中,成功后,添加如下代码:
success: function(response) {
alert(response);
},
error: function(response) {
console.log(response.status + " " + response.statusText);
}
That will print in your console a clue to what is causing this error.
这将在您的控制台中打印出导致此错误的原因的线索。
By the way, there are some other suggestions, your validations can be achieved with the new HTML5 input types (email, phone), if you have to maintain compatibility with browsers that don't support these, you can find a jQuery plugin that handles this.
顺便说一句,还有一些其他的建议,你的验证可以用新的 HTML5 输入类型(电子邮件、电话)来实现,如果你必须保持与不支持这些的浏览器的兼容性,你可以找到一个 jQuery 插件来处理这。
回答by user1345344
The same thing happened to me.And I used the same version of JQuery (1.7.1) And the weirdest thing is that after adding "asyn:false ",it worked out. I guess this might be a bug of JQuery.
同样的事情发生在我身上。我使用了相同版本的 JQuery (1.7.1) 最奇怪的是,在添加“asyn:false”之后,它解决了。我猜这可能是 JQuery 的一个错误。
回答by ecruz
I'm not sure about using $(this).serialize(). Have you tried using $('.collector').serialize() (or whichever the form is) since inside the ajax request the context may change. It's just a quick guess, hope it helps.
我不确定是否使用 $(this).serialize()。您是否尝试过使用 $('.collector').serialize() (或任何形式),因为在 ajax 请求中,上下文可能会改变。这只是一个快速的猜测,希望它有所帮助。