Jackal
Jackal

Reputation: 25

cant get data with json jquery

trying to get data by Id of label here is the script;

 $(document).on('click', '.call', function () {
        var nId = $(this).data("id")
        GetText(nId);
    });

    function GetText(id) {
        var callid = id;
        alert(callid);
        $.ajax({
            dataType: "json",
            type: "POST",
            contentType: "application/json",
            url: '@Url.Action("TextData","Home")',
            data: "{'callid':'" + callid+ "' }",
            success: function (data) {
                if (data.d != null) {
                    $("#text").html(data.d);

                }
                else {

                }

            },
            error: function () {
                alert("error");
            }
        });

        return false;

    }

and here is controller ;

public JsonResult TextData()
    {
        var model = Db.texttable.Select(s => new HomeModel.Content
            {
                text=s.text,
            });

        return Json(model, JsonRequestBehavior.AllowGet);
    }

I can get Id but I cannot get text into the label called #text in success how can I fix and also do it with less code

here is my list ;

 @foreach (var item in Model.AyetContent)
  {
    <div><i class="fa fa-share"></i>@item.Baslik</div>
                                    @Html.Raw(item.Text)
    <div>
    @foreach (var itemDesc in Model.Ayetler.Where(o => o.Ayet == item.Id))
    {
    <div><span style="cursor:pointer;float:right;" class="call" data-id="@itemDesc.Id"><i class="fa fa-random"></i> @itemDesc.Baslik <br /></span></div><br />
     }
     </div>
     }

here is the part that should reach

  <div class="post">
  <label id="text"></label>
   </div>

I made this instead of upper jquery

 $.ajax({
            url: '@Url.Action("TextData", "Home")',
            type: "GET",
            success: function (result) {
                $("#text").append(result);
                alert(result);
            }

now it returns [object Object][object Object]

Upvotes: 1

Views: 39

Answers (1)

beautifulcoder
beautifulcoder

Reputation: 11320

Should be:

$("#text").append(result.text);

Also, you only need one record and an anonymous object:

Db.texttable.Select(s => new { text = s.text }).First();

Upvotes: 2

Related Questions