Reputation: 3
Hello i'm having problems sending data through ajax to my asp.net controller. When i try to view the problem in opera inspector i get server 500 error on jquery line 8720. xhr.send( ( s.hasContent && s.data ) || null ); Here are my code samples: Html:
<div class="top-row">
<div class="field-wrap">
<label>
<span class="req"></span>
</label>
<input class="text-center" type="text" required autocomplete="off" name="url" id="url" />
</div>
</div>
<button type="submit" class="button button-block" name="sendIT" id="sendIT">Save</button>
</div>
Javascript:
var button1 = document.getElementById("sendIT");
function LoginButton1OnClick() {
var text = $('#url').val();
alert(text);
$.ajax({
type: 'POST',
url: '/Book/TestBook',
crossDomain: true,
data: text,
success: function () {
alert('success');
}, error: function (data) {
alert("Error");
}
});
}
button1.addEventListener("click", LoginButton1OnClick);
C#:
[HttpPost]
enter code here
public ActionResult TestBook(string test)
{
var s1 = test;
return View();
}
Upvotes: 0
Views: 3566
Reputation: 5732
The error 500 you're getting is because of the malformed 'data' in your ajax POST request. You need to send the data as an object:
Javascript
$.ajax({
type: 'POST',
url: '/Home/TestBook',
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({test: text }),
success: function (data) {
alert('success');
}, error: function (data) {
alert("Error");
}
});
Note that the "test" parameter in javascript needs to mach the exact same name in your controller.
Upvotes: 0
Reputation: 536
you need to post data as object
$.ajax({
type: 'POST',
url: '/Book/TestBook',
crossDomain: true,
data: {test: text},
success: function () {
alert('success');
}, error: function (data) {
alert("Error");
}
});
Upvotes: 1