我想用Ajax jquery提交一个表单,
我想把数据从http://domain1.com发送到http://domain2.com
这是我的表格
<form action="https://domain2.com/index.php/site/send_sms" method="post">
<input type="hidden" name="with_slogan" value="yes" id="with_slogan" />
<input type="hidden" name="glist" id="glist" value="" />
Number: <br/>
<input type="text" name="to_mobile" value="" id="to_mobile" class="hide" />
<br/>
Message: (100 text) <br/>
<textarea cols="30" rows="6" maxlength="110" name="sms_body" id="sms_body"> --shaikatssj </textarea>
<br/>
<input type="submit" name="submit" value="Send" class="sm-button-submit" /></form>
Ajax: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script> var url="https://domain2.com/index.php/site/send_sms"; var datastring = "with_slogan=yes&glist=&to_mobile=017147974&sms_body=teshejdsgjkjfdt&submit=submit"; $.ajax({ url: url, data: datastring, type: "POST", crossDomain: true, success: function(e) { alert(e.responseText); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Status: " + textStatus); alert("Error: " + errorThrown); } }); </script>
我尝试ajax后,但获得零成功。
- 首先在你的表单中,你不需要action和method post,你是通过ajax脚本发送的,所以删除它
- 添加表单id表单id='form'
试着用这个脚本:
$('#form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'yourpage.php',
data: $('#form').serialize(),
success: function(){
alert('success');
},
});
});
});
});