简体   繁体   English

为什么不在jQuery的Ajax POST中获取POST参数

[英]why not get POST parameters in ajax POST of jQuery

The code is listed below: 下面列出了代码:

    $.ajax({
     type: "POST",
     url: "http://localhost:3000/rcm/global_config/update",    
     data: {k: 'sdfa', v: 'dsfas'},
     success: function(data, textStatus, XMLHttpRequest){
       alert("数据更新成功");
     },
     error: function(xhr,textStatus, errorThrown){
       alert("数据更新失败,请刷新回滚");
     }
    });

In the server i can not get post parameters, and then i tamper the request sent by ajax, it doesn't send the data params at all. 在服务器中,我无法获取post参数,然后篡改ajax发送的请求,它根本不发送数据参数。 i don't know where i am wrong. 我不知道我在哪里错。

thank you in advance. 先感谢您。

This issue has been asked and aswered before in SO jquery-ajax-post-sending-options-as-request-method-in-firefox 这样的问题已在SO Jquery -ajax-post- send -options-as-request-method-in-firefox中被询问和提出

This is because the same origin policy on FF. 这是因为在FF上具有相同的来源策略。 It only allows you to do XMLHTTPRequests to your own domain. 它仅允许您对自己的域执行XMLHTTPRequests。 Maybe your script is loaded from domain "localhost" and your ajax request to "localhost:3000" 也许您的脚本是从域“ localhost”加载的,而您的ajax请求已加载到“ localhost:3000”

Please try this 尝试这个

$.ajax({
 type: "POST",
 url: "http://localhost:3000/rcm/global_config/update",    
 data: "{'k': 'sdfa', 'v': 'dsfas'}",       // Change are here in this line
 success: function(data, textStatus, XMLHttpRequest){
   alert("数据更新成功");
 },
 error: function(xhr,textStatus, errorThrown){
   alert("数据更新失败,请刷新回滚");
 }
});
   $.ajax({
     type: "POST",
     url: "http://localhost:3000/rcm/global_config/update",    
     data: $.param({k: 'sdfa', v: 'dsfas'}),
     success: function(data, textStatus, XMLHttpRequest){
       alert("数据更新成功");
     },
     error: function(xhr,textStatus, errorThrown){
       alert("数据更新失败,请刷新回滚");
     }
    });

Wrapping the data object in $.param should do it. 将数据对象包装在$ .param中即可。 It will convert that object to the string "k=sdfa&v=dsfas" 它将将该对象转换为字符串“ k = sdfa&v = dsfas”

hi please try somthing like this: 嗨,请尝试这样的事情:

$.ajax({
        type:"POST",
        url:"student/info/ajax.php",  
        data:({type:'test', r:which}),
        success:function(data){
            if((data.result)=='true')
                $('#result_popup').html(data.output);
            }, 
        dataType:"json"});
        return false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM