Ajax发布不发送数据 [英] ajax post not sending data

查看:167
本文介绍了Ajax发布不发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我想通了.是一个URL问题,它在发送到服务器之前重定向并清除了POST.

UPDATE: I Figured it out. Was a URL issue, which redirected and cleared the POST before it was sent to the server.

$('#addbtn').on('click',function() {

$.ajax({
  type: "POST",
  url: "/create/",
  dataType: "json",
  data: $('#MultiAdd').serializeArray(),
  success: function (data) {
    // this returns Failed. Please Try Again. c_name=
    // c_name should equal the value from post. 
    alert(data.msg)
  },
  error: function (xhr, ajaxOptions, thrownError) {}
 });

});

我似乎无法获得此信息来发布数据.香港专业教育学院尝试了很多变化.看了几个小时试图找出问题所在.

I can not seem to get this to post the data. Ive tried so many variations. Looking at this for hours trying to see whats wrong.

我尝试了data: {test:'test'}(没有用)

GET函数可以,但是我需要将它设为POST. 我也尝试了普通的.serialize().仍然没有工作.

the GET function does, but I need this to be POST. I have also tried normal .serialize(). Still didnt work.

这确实可以向我显示值.但是ajax不会在提交中发布它们.

This does work to show me the values. but ajax will not POST them in the submission.

console.log($('#MultiAdd').serializeArray());

(在页面上,表单和ajax代码是动态添加的,但我认为不会有太大变化,我已经对其进行了测试.)

(on the page, the form and ajax codes are dynamically added, but I dont think that would change much, Ive tested that already.)

更新:(我正在使用codeigniter)-服务器端.尝试访问[input name ="c_name"]

Update: (i'm using codeigniter) - Server-side. Trying to access the [input name="c_name"]

public function create()
{
   $value = $this->input->post('c_name');

   $msg = array("msg"=>"Failed. Please Try Again. c_name=".$value); 

   die(json_encode($msg)); 
 }

推荐答案

如果GET请求有效,但POST无效,则通常是重定向问题.就是说,尽管请求最初访问了第一个URL并被重定向到另一个URL,但是所有POST数据都没有转发到另一个URL.因此,所有数据都会丢失.

If GET request works but POST doesn't it's most usually the issue with redirect. That said, while the request initially accessed the first URL and got redirected to another URL then all the POST data were not forwarded along to another URL. Therefore all the data gets lost.

可能会发生一些常见情况(取决于服务器配置):

Some common cases that might occur (depending on server configuration):

  • "/方法/" 可能会被您的网站重定向到"/somethingCompletelyElse/"
  • "/方法" 可能会被您的站点重定向到"/en/方法" (语言重定向)
  • 方法/" 可能会被您的网站重定向到方法"
  • 方法" 可能会被您的网站重定向到方法/" (常见的,额外的反斜杠)
  • "example.com/method" 可能会被您的网站重定向到"www.example.com/method"
  • "/method/" might get redirected by your site to "/somethingCompletelyElse/"
  • "/method" might get redirected by your site to "/en/method" (language redirect)
  • "method/" might get redirected by your site to "method"
  • "method" might get redirected by your site to "method/" (common, extra backslash)
  • "example.com/method" might get redirected by your site to "www.example.com/method"

您明白了.如果您在网络面板中查看,发现没有发送数据,但响应为200 OK,那么最好检查一下网络面板中的 Request URL 在您的代码中指定.尽管只有在现代浏览器中才有可能.在较旧的浏览器中,您甚至都不知道URL已被重定向.

You get the idea. If you look in network panel and notice that there is no data sent but response was 200 OK then it's good idea to check if Request URL in network panel is the one that you specified in your code. Although this will be possible only in modern browsers. In older browsers you won't even know that URL has been redirected.

例如(请参见我的请求网址中缺少"en/"):

For Example (see that "en/" is missing in my request url):

这篇关于Ajax发布不发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆