2

I am trying to post some HTML information to a url using the ajax post command

var html = "<b>bold</b>";

$.ajax({
    type: "POST",
    url: "/DragDrop/GetData/" + id + "?html=" + html,
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
    }
});

But of course you can't pass html in the URL, I know this is something easy but it is driving me nuts, how do I do this?

2
  • You're posting JSON in the POST request? Really? Is the server-side script expecting that? Are you sure you don't mean to do a normal application/x-www-form-urlencoded submission, with data: {html: html} to let jQuery form-encode the content for you? Commented Aug 11, 2010 at 10:47
  • yes you are correct, thanks. This appears to be working for the html just as text, but when I add any tags, it does not get passed through, any ideas? Commented Aug 11, 2010 at 11:55

2 Answers 2

2

You are doing a POST. Why are you trying to pass the content in the URL instead of using data?

var html = "<b>bold</b>";

$.ajax({
    type: "POST",
    url: "/DragDrop/GetData/" + id,
    data: html,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, this would work. Issue I have now is on the c# side my public ActionResult GetData(string id, string html); The variables are all null?
What does your route for GetData look like? You can also try data : {html : html} in your ajax call.
yes that is correct, it does not allow content that has tags in e.g. Hello <b>world</b> it gets ignored
Have you tried escaping the string? var html = escape("<b>world</b>").
1

If you really wanted to pass it through query string, you would have to encode it. Here's a jQuery package for encoding it:

http://plugins.jquery.com/project/URLEncode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.