javascript - Ajax post call returns parsererror - Stack Overflow

I submit the reactjs form as mentioned below,submit(){if(this.checkRequiredField()){$.ajax({url: &#

I submit the reactjs form as mentioned below,

submit(){    
    if(this.checkRequiredField()){
    $.ajax({
      url: '/api/Accounts',
      dataType: 'json',
      type: 'POST',
      data: {               
          Name: this.state.name, 
          StartDate :this.state.startDate, 
          EndDate : this.state.endDate,
          UserCount: this.state.userCount          
        },
      success: function(data, status, xhr) {            
        console.log('data added successfully');
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(status, err.toString());
      }.bind(this)
    })
  }

The above ajax post will call the respective Web Api post method, where the data got inserted successfully to the db.

After it posts the data, the program doesn't return to the success function, instead it calls error function and logs the error

parsererror SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

when I checked the xhr.status, the value is 201 and statustext is Created.

Why the above ajax post call returns parsererror? how to fix this issue?

I submit the reactjs form as mentioned below,

submit(){    
    if(this.checkRequiredField()){
    $.ajax({
      url: '/api/Accounts',
      dataType: 'json',
      type: 'POST',
      data: {               
          Name: this.state.name, 
          StartDate :this.state.startDate, 
          EndDate : this.state.endDate,
          UserCount: this.state.userCount          
        },
      success: function(data, status, xhr) {            
        console.log('data added successfully');
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(status, err.toString());
      }.bind(this)
    })
  }

The above ajax post will call the respective Web Api post method, where the data got inserted successfully to the db.

After it posts the data, the program doesn't return to the success function, instead it calls error function and logs the error

parsererror SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

when I checked the xhr.status, the value is 201 and statustext is Created.

Why the above ajax post call returns parsererror? how to fix this issue?

Share Improve this question edited Nov 3, 2016 at 4:45 Shubham Khatri 282k58 gold badges431 silver badges411 bronze badges asked Nov 3, 2016 at 4:30 MemoryLeakMemoryLeak 5253 gold badges8 silver badges20 bronze badges 6
  • 2 /api/Accounts isn't returning valid JSON – Jaromanda X Commented Nov 3, 2016 at 4:31
  • should I remove the dataType:'json' from the above code? – MemoryLeak Commented Nov 3, 2016 at 4:33
  • Do you expect JSON? console.log(xhr.responseText) or look at the request in the network panel to see what was returned. – epascarello Commented Nov 3, 2016 at 4:34
  • 1 what does /api/Accounts return? dataType:'text', is a safe option I guess – Jaromanda X Commented Nov 3, 2016 at 4:34
  • 1 201 don't have response body usually. Remove that dataType option and it will be fine. – Mat J Commented Nov 3, 2016 at 4:41
 |  Show 1 more ment

3 Answers 3

Reset to default 6

The problem is with the dataType mentioned in the ajax call.

The post method is not returning any json data, by changing the dataType :'json' to dataType:'text' fixed the issue.

Thanks Jaromanda X and Mathew Jibin for your inputs

If you are not expecting a JSON format return, you may need to remove that instead.

submit(){    
    if(this.checkRequiredField()){
    $.ajax({
      url: '/api/Accounts',
      type: 'POST',
      data: {               
          Name: this.state.name, 
          StartDate :this.state.startDate, 
          EndDate : this.state.endDate,
          UserCount: this.state.userCount          
        },
      success: function(data, status, xhr) {            
        console.log('data added successfully');
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(status, err.toString());
      }.bind(this)
    })
  }

just a suggestion, if you want to return a json from controller you can pass a custom message from controller, like saved successfully, keeping your same ajax code

  success: function(data, status, xhr) {            
        console.log(data.ResponseText );
      }

controller: based on condition

       return Json(new { success = true, 
ResponseText = "saved successfully" }, JsonRequestBehavior.AllowGet);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742419751a4440443.html

相关推荐

  • javascript - Ajax post call returns parsererror - Stack Overflow

    I submit the reactjs form as mentioned below,submit(){if(this.checkRequiredField()){$.ajax({url: &#

    16小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信