【问题标题】:POST method in AJAXAJAX 中的 POST 方法
【发布时间】:2014-01-26 07:41:47
【问题描述】:

我正在使用 post 方法传递变量,但无法传递变量的完整值。

这是脚本:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

    var problem_code = $("#problem_code").val();
    var code = $("#code").val();
    var lang = $("#lang").val();
    $.ajax({
        type: "POST",
        url: "ajax.php",
        cache:false,
        data : {problem_code:problem_code,code:code,lang:lang},
        success: function(html)
        {
            //alert("ajax response returned.. ");
            //console.log(html);
            $(".show-result").empty();
            $(".show-result").append('<br>' + html);
        }
    })

});
</script>

这是剩下的部分

<?php 
$code=stripcslashes($_POST['code']);
$lang=stripcslashes($_POST['lang']);
?>

通过另一个文件“Submit.php”获取$code和$lang值

现在使用输入隐藏字段

    <input type="hidden" id="problem_code" name="problem_code" value=<?php echo $problem_code;?> >
<input type="hidden" id="code" name="code" value=<?php echo ($code);?> >
<input type="hidden" id="lang" name="lang" value=<?php echo $lang;?> >

但在使用隐藏字段传递 $code 值时,它会显示意外行为。

这是运行上述脚本时的查看源代码示例

<input type="hidden" id="problem_code" name="problem_code" value=LUCPAL >
<input type="hidden" id="code" name="code" value=#include <iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main() {
int t;
scanf("%d",&t);
while(t--)
{
    long long int A,B,copy,sum=0;
    scanf("%lld %lld",&A,&B);
    copy=B;
    int digit=0;
    while(copy>0)
    {
        digit++;
        copy=copy/10;
    }
    while(digit--)
    {
        sum=10*sum+9;
    }
    long long int sum2=sum/2,diffA,diffB;
    if(sum2>=A && sum2<=B)
    {
        printf("%lld
",(sum2)*(sum2+1));
    }
    else
    {
        diffA=fabs(sum2-A);
        diffB=fabs(sum2-B);
        if(diffA<diffB)
            printf("%lld
   ",A*(sum-A));
        else
        printf("%lld
    ",B*(sum-B));

    }
}
// your code goes here
return 0;
}
>

<input type="hidden" id="lang" name="lang" value=11 >
`
    printf("%lld\n",A*(sum-A) become  printf("%lld

    ",A*(sum-A));

并且只有 '#include' 从所有代码中传入 ajax.php。

【问题讨论】:

  • 你知道你应该在你的 jquery 开启器中使用
  • 没有@AndrewAllenWest 我应该只使用
  • 什么意外行为?
  • @AndrewAllenWest:这仅适用于 HTML5 - XHTML 是正确的语法,也许我遗漏了一些东西,但我没有看到任何关于版本的提及
  • @KundanSinghChouhan 当我在文件中打印 $code 的值时,它正在打印通过表单传递的正确代码,但在通过输入隐藏字段传递给 ajax.php 时,只有“#include”,其余的代码没了?

标签: php jquery ajax forms post


【解决方案1】:

&amp;gt;&amp;lt;替换你所有的(无论如何在值=“...”部分) 发布请求会认为您错误地关闭了标签并在第一个 >(小于)符号处切断。 编辑: 像这样:

<input type="hidden" id="problem_code" name="problem_code" value=LUCPAL >
<input type="hidden" id="code" name="code" value='#include &gt;iostream&lt;
#include&gt;stdio.h&lt;
#include&gt;math.h&lt;
using namespace std;
int main() {
int t;
scanf("%d",&t);
while(t--)
{
    long long int A,B,copy,sum=0;
    scanf("%lld %lld",&A,&B);
    copy=B;
    int digit=0;
    while(copy&lt;0)
    {
        digit++;
        copy=copy/10;
    }
    while(digit--)
    {
        sum=10*sum+9;
    }
    long long int sum2=sum/2,diffA,diffB;
    if(sum2&lt;=A && sum2&gt;=B)
    {
        printf("%lld
",(sum2)*(sum2+1));
    }
    else
    {
        diffA=fabs(sum2-A);
        diffB=fabs(sum2-B);
        if(diffA&gt;diffB)
            printf("%lld
   ",A*(sum-A));
        else
        printf("%lld
    ",B*(sum-B));

    }
}
// your code goes here
return 0;
}'
>

<input type="hidden" id="lang" name="lang" value=11 >
`
    printf("%lld\n",A*(sum-A) become  printf("%lld

    ",A*(sum-A));

【讨论】:

  • 你的意思是我应该把所有的 转换成 >和 <在源代码中对吗?以及关于换行符的内容?
  • 仅在值部分 - 我认为换行应该没问题
  • 在我注意到 iostream> 关闭
猜你喜欢
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 2015-12-16
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
相关资源
最近更新 更多