I have a Controller Action that is trying to return plain text to an AJAX call. E.G.-
[HttpPost]
public ActionResult SubmitAttachment(int id, HttpPostedFileBase theFile){
...
...
return Content("Success");
}
In my javascript function which is doing the ajax call, I am expecting the response text to just be "Success", but I'm getting "<head></head><body>Success</body>"
instead.
In 99% of my other controller actions, I don't have this issue. The only thing that I can think of that makes this different is because the AJAX call to "SubmitAttachment" is a form POST with the "enctype=multipart/form-data", where as the other AJAX calls aren't uploading any files.
Has anyone encountered this before? If so...how would I just have it return plain text开发者_StackOverflow中文版?
NOTE: I have also tried return Content("Success", "text/plain")
, but that just adds additional <pre></pre>
tags around the "Success" string.
Unable to reproduce the behavior:
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(int id, HttpPostedFileBase theFile)
{
return Content("Success", "text/plain");
}
}
View:
<script type="text/javascript" src="<%: Url.Content("~/scripts/jquery-1.4.1.js") %>"></script>
<script type="text/javascript">
$(function () {
$('form').submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
alert(result);
}
});
return false;
});
});
</script>
<form action="/" method="post" enctype="multipart/form-data">
<input type="text" name="id" value="4" />
<input type="submit" value="OK" />
</form>
When the form is submitted the server returns only "Success"
.
I would do what bzlm suggested, using fiddler to test it out to make sure those tags are actually coming back that way. If they still are, try returning a plain string instead. Set the return type to string and then return "success" without the Content(). If you are still getting tags around it when returning a string then you are in an alternate reality where things that are not possible are somehow possible.
精彩评论