0

I need to post JSON data from the client to the application ASP.NET MVC 4, but the javascript code is run by user and transfer data from the site www.lotsalneschs.com to my server. For testing I used Google Chrome console panel. Controller code:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public JsonResult Save(List<NewsModel> items)
    {
        return Json(null);
    }
}

public class NewsModel
{
    public string Title { get; set; }

    public string Content { get; set; }

    public List<string> Tags { get; set; }
}

Javascript code:

function News() {
    var arr = {};
    var title = "items";
    var tTitle = "Title";
    var cTitle = "Content";
    var tgTitle = "Tags";
    arr[title] = [];
    for (var i = 0; i < 10; i++) {
        var n = {};
        n[tTitle] = "Title №" + i;
        n[cTitle] = "TEXT";
        n[tgTitle] = [];
        for (var j = 0; j < 5; j++) {
            n[tgTitle].push("tag" + j);
        }
        arr[title].push(n);
    }
    return arr;
}
var news = News();
$.ajax({
    url: 'http://localhost:28369/Home/Save',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: JSON.stringify(news)
});

If I execute the script while being on the page localhost:28369/Home/Index everything works perfectly:

https://i.stack.imgur.com/RL1nR.png

  But the execution of this script on any other page, for example stackoverflow.com does not break at the same breakpoint. If I delete contentType in the script, get the following:

https://i.stack.imgur.com/4Hcyn.png

If I delete contentType and not use JSON.stringify to format my data, get the following:

https://i.stack.imgur.com/F1Ql7.png

How to fix this problem?

tereško
  • 58,060
  • 25
  • 98
  • 150
Jeksonic
  • 123
  • 8

1 Answers1

0

I think it has to do with your URL being hard coded to localhost. Try replacing your line

url: 'http://localhost:28369/Home/Save',

with

url: '@Url.Action("Save", "Home")',

Edit:

Thinking about it you should see what path is generated by the Url.Action. I think the issue is being run from a different server local host is pointing to that server and not your computer. If the Url.Action doesn't work then you should point it to a public facing address.

Matt Bodily
  • 6,403
  • 4
  • 29
  • 48
  • My javascript code is not belong to ASP.NET MVC 4 application. I run it myself in the Console Google Chrome, because I can not use @Url.Action("Save", "Home"). – Jeksonic Sep 17 '13 at 01:13
  • I need to post JSON data being on the any page, which not belong to my ASP.NET MVC 4 application – Jeksonic Sep 17 '13 at 01:23
  • I think the issue you are having is related to your return url being localhost. That will just point at the server you are running your script on and not your computer. You need to change that return url to an address on your server that the remote server can see. http://www.yourdomain.com/save something like that – Matt Bodily Sep 17 '13 at 14:42
  • I thought so, but in the end I send a request to a server that is on the same computer. To all doubts about unavailable of server disappeared, I hosted my ASP.NET MVC 4 application. Accordingly, in the script I change the url to the my public website address. And nothing has changed. If I run the script while being on the page /Home/Index, the data received in the correct format. If I run the script on the page http://stackoverflow.com/, nothing happens. – Jeksonic Sep 17 '13 at 19:14
  • I am developing a Google Chrome extension that would send data from a particular webpage to my server. I have read that a request with XMLHttpRequest can only be made to addresses with the same protocol, domain, port, as the current page. But If I run the script on the page stackoverflow.com after removing the script parameter contentType, the data received on the server, but is not able to properly deserialize the data and List items is equal null. – Jeksonic Sep 17 '13 at 19:45
  • That is great you are able to get data now. I have no idea why the format would be different from a different computer. You might try changing your received parameter to just a string or something that will not be null and try to parse it from there. Good Luck! – Matt Bodily Sep 17 '13 at 20:12
  • Thanks, most likely I will do so. But it's really weird. I find the same strange case http://stackoverflow.com/a/9010760/2784219, but there aren't any explanation. – Jeksonic Sep 17 '13 at 21:32