我叫使用$阿贾克斯和$不用彷徨和得到两个不同的结果相同的PHP脚本。
$。从通过函数传递,而$不用彷徨没有数据AJAX条换行符。
我已经明确地尝试设置数据类型设置为文本和HTML,没有运气。大多数$就以相同的参数为$不用彷徨默认的。
http://api.jquery.com/jQuery.ajax/
这是一个错误?
下面是确切的code我用:
$。获得(LIB / ajax_scripts /套,产品value.php,{input_id:input_id,VAL:值});
$阿贾克斯({
网址:LIB / ajax_scripts /套,产品value.php
键入:GET,
数据:'input_id ='+ input_id +'和; VAL ='+值});
下面是code任何人都可以试试谁有权访问支持PHP的服务器和萤火虫。看看每个请求的萤火虫的反应,你会看到< BR />
添加到$不用彷徨,而不是$就
ajaxtest.html
<形式方法=GET的onsubmit =返回false>
<文字区域的id =数据NAME =数据>将
b
C< / textarea的>
<输入类型=提交值=提交ID =提交>
< /形式GT;
<脚本类型=文/ JavaScript的SRC =http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$('#提交)。点击(函数(){
。VAR数据= $('#数据)VAL();
$获得(data.php,{数据:数据});
$阿贾克斯({
网址:data.php
键入:GET,
数据:数据='+数据});
});
< / SCRIPT>
data.php
<?PHP的回声nl2br($ _ GET ['数据']); ?>
解决方案
$阿贾克斯返回的是缺少换行符,当我追加给我的HTML文本。因为我想保存的格式,我不得不把标签了。所以我使用了字符串函数替换这样的:
无功第一部分= / \ N /克;
VAR newc_data = old_data.replace(第1部分,< BR>);
这可能是一个很好的理由,以避免文本的格式。但是,在我的情况下,我想写一些情报将文本转换为JSON。
I call the same PHP script using $.ajax and $.get and get two different results.
$.ajax strips line breaks from data passed through the function while $.get doesn't.
I've tried explicitly setting the dataType to text and html with no luck. Most of the parameters for $.get default to the same in $.ajax.
http://api.jquery.com/jQuery.ajax/
Is this a bug?
Here is the exact code I use:
$.get("lib/ajax_scripts/set-product-value.php", { input_id: input_id, val:value });
$.ajax({
url:"lib/ajax_scripts/set-product-value.php",
type:'GET',
data:'input_id='+input_id+'&val='+value});
Below is code anyone can try who has access to PHP enabled server and firebug. Look at the firebug response for each request, you will see that <br />
are added to the $.get and not to $.ajax.
ajaxtest.html
<form method="GET" onsubmit="return false">
<textarea id="data" name="data">a
b
c</textarea>
<input type="submit" value="Submit" id="submit">
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
var data = $('#data').val();
$.get("data.php", { data: data });
$.ajax({
url:"data.php",
type:'GET',
data:'data='+data});
});
</script>
data.php
<?php echo nl2br($_GET['data']); ?>
解决方案
$ Ajax returned text that were missing line breaks when I appended it to my html. Since I wanted to save the formatting, I had to put tags in it. So I used the string function replace like this:
var part1 = /\n/g;
var newc_data = old_data.replace(part1,"< br >") ;
This is probably a good reason to avoid text as a format. But in my case I want to write some intelligence to convert text to json.