Jquery ajax post request not working

2024/11/14 13:45:05

I have a simple form submission with ajax, but it keeps giving me an error. All the error says is "error". No code, no description. No nothing, when I alert it when it fails.

Javascript with jQuery:

$(document).ready(function(){$(".post-input").submit(function(){var postcontent = $(".post-form").val();if (postcontent == ""){return false;}$(".post-form").attr("disabled", "disabled");$.ajax({url: '/post',type: 'POST',data: {"post-form": postcontent},dataType: json,success: function(response, textStatus, jqXHR) {alert("Yay!");},error: function(jqXHR, textStatus, errorThrown){alert(textStatus, errorThrown);}});});});

HTML:

<form class="post-input" action="" method="post" accept-charset="utf-8"><textarea class="post-form" name="post-form" rows="1" cols="10" onFocus="this.value='';return false;">What are you thinking about...</textarea><p><input class="post-submit" type="submit" name = "post.submitted" value="Post"></p></form>

and if there are no problems there, then the server-side (pyramid):

def post(request):session = Session()user = authenticated_userid(request)postContent = request.POST['post-form']if not postContent == '':session.add(Activity(user.user_id, 0, postContent, None, None))return {}return HTTPNotFound()

UPDATE: After some more debugging with firebug, I discovered that the post request body contains only post.submitted=Post, instead of the intended result of {"post-form": postcontent}.

Answer

According to jQuery documentation, you must declare the data type:

$.ajax({type: 'POST',url: url,data: data,success: success,dataType: dataType
});

Also, looking at your server-side code, you don't actually want to post JSON formatted data. This {"post-form":postcontent} is JSON formatted data. What you actually want to do is send TEXT or HTML. Seeming as it's form data, I would guess at TEXT.

Try this:

$.ajax({url: '/post',type: 'POST',data: 'post-form='+postcontent,dataType: 'text',success: function(response, textStatus, jqXHR) {alert("Yay!");},error: function(jqXHR, textStatus, errorThrown){alert(textStatus, errorThrown);}
});
https://en.xdnf.cn/q/72097.html

Related Q&A

Why is the main() function not defined inside the if __main__?

You can often see this (variation a):def main():do_something()do_sth_else()if __name__ == __main__:main()And I am now wondering why not this (variation b):if __name__ == __main__:do_something()do_sth_e…

Using selenium inside gitlab CI/CD

Ive desperetaly tried to set a pytest pipeline CI/CD for my personal projet hosted by gitlab. I tried to set up a simple project with two basic files: file test_core.py, witout any other dependencies f…

VSCode issue with Python versions and environments from Jupyter Notebooks

Issue: I am having issues with the environment and version of Python not matching the settings in VSCode, and causing issues with the packages I am trying to use in Jupyter notebooks. I am using a Wind…

weighted covariance matrix in numpy

I want to compute the covariance C of n measurements of p quantities, where each individual quantity measurement is given its own weight. That is, my weight array W has the same shape as my quantity ar…

How to implement biased random function?

My question is about random.choice function. As we know, when we run random.choice([apple,banana]), it will return either apple or banana with equal probabilities, what if I want to return biased resul…

Finding minimum value for each level of a multi-index dataframe

I have a DataFrame that looks like this:data a b 1 1 0.12 0.23 0.3 2 1 0.52 0.63 0.7and I want to find the minimum value for each level of a ignoring the b level, so as an output Im l…

Count occurrences of a list of substrings in a pyspark df column

I want to count the occurrences of list of substrings and create a column based on a column in the pyspark df which contains a long string.Input: ID History1 USA|UK|IND|DEN|MAL|SWE|AUS2…

What are screen units in tkinter?

I was reading the response in the link below and ran into screen units but I couldnt find what exactly was referred to by screen units in Jim Denneys response. I know they are not pixels. How do I use …

Python SUMPRODUCT of elements in nested list

I have two nested lists: a = [[1,2,3],[2,4,2]] b = [[5,5,5],[1,1,1]]I want to multiply and SUMPRODUCT each group of elements to get c = [[30],[8]]Which result from = [[1*5+2*5+3*5],[2*1,4*1,2*1]] Ive t…

Modifying viridis colormap (replacing some colors)

Ive searched around and found things that came close to working but nothing exactly suiting what I need. Basically, I really like the viridis colormap as a starting point. However, I would like to repl…