ajax error code,7. Warning: Ajax error

7. Warning: Ajax error

When using the ajax option to load data for DataTables, a general error can be triggered if the server responds with anything other than a valid HTTP 2xx response. The error given is:

DataTables warning: table id={id} - Ajax error

where {id} is the DOM id of the table that triggered the error.

Meaning

This occurs when jQuery falls into its error callback handler (this callback built into DataTables), which will typically occur when the server responds with anything other than a 2xx HTTP status code. For example the server might respond with 404 Not Found indicating that the file requested is not available at the given URL, or 500 Internal Error which indicates that the server encountered an error while processing the request.

DataTables will fire a specific error for the case where the request from the server is a valid return (200 Ok for example), but not valid JSON - see technical note 1 for more information. This error (7) indicates a general error, as stated above.

Diagnosis

If the server didn't reply to the Ajax request with a 2xx status code, we need to know what it did reply with, so we can take corrective action. So discovering what that reply was will be the starting point for resolving the issue full.

Modern browsers all come with built in developer tools, which can be used to find out what data the server is responding to the DataTables Ajax request with. Instructions on how to do this with your browser are shown below.

Safari

e240a21bcacfafa793b3750b2750eedc.png

1. Open preferences

9ad9366abef1fbd7171aa3feaba45a02.png

2. Enable developer menu

fc0a705379f3c7b08e09da750913388b.png

3. Show error console

3c09fc492e4ae55ebe559b501db620a3.png

4. The error console

715bd83179fa096ab5d2659c993c552b.png

5. Ajax response

By default Safari hides its built-in developer tools. If you don't have a Develop menu shown in Safari it needs to be enabled. If you do, jump to step 3.

Open the Safari menu and select Preferences...

Enable the developer menu in the Advanced tab

Click the Advanced tab in the Preferences window

Check the box for Show Develop menu in menu bar option

Close the preferences - the Develop menu will now be shown

View the error console

Select the Show Error Console option from the Develop menu.

The developer console will show at the bottom of the page

Any errors encountered on the page are shown in the developer console

Safari logs general Ajax errors to the developer console - in the example shown above a 404 Not Found error.

Additional information can also be found in the Resources tab.

Click the Resources tab

Reload the page so Safari can capture all network requests

Safari will show a list of all resources (images, scripts etc) which have been loaded for the page. Locate the Ajax request that has been made to the server and click it. Safari will show detailed information about the request.

Note that on pages with a large number of resources, Safari will group the resources by type into folders. If this happens on your site, look for the "XHRs" folder for the Ajax requests.

Please note that these instructions were written with Safari 7 as the version used. Newer versions might have slightly different steps.

Chrome

a2526103e056f40dc631979b5650093f.png

1. Open debugger

52633dc789609ea7a7cda0fc8a79ab99.png

2. Console displayed

8d55e20b8625f3a15e1ff6cb784a0e57.png

3. Show Network tab

9226809a987d63cdccca3d3fc95c416c.png

4. Ajax response

To access the Chrome's developer console:

Mac: Access the View > Developer > JavaScript Console menu option.

Windows: Click the Chrome menu (to the right of the address bar) and select Tools > Javascript console.

The Console_ is shown at the bottom of the current browser window.

If a general error has occurred, it will be shown here

More detailed information can be obtained from the network panel will show all of the requests made by Chrome to load the page.

Refresh the page to allow Chrome to capture all requests

Click the XHR option at the bottom of the window to reduce the requests to just the Ajax (XHR) requests.

Details about the Ajax request are shown.

Double click the Ajax requests made, including the one which is resulting in an error - a 404 error in this case.

Please note that these instructions were written with Chrome 31 Mac as the version used. Newer versions, or those on different platforms might have slightly different steps.

Firefox

5152e7689e0307527a51726df65346af.png

1. Open debugger

b7875d4a6510c6ae3bccb2a54ab523b0.png

2. Network requests

74ef81ab5419bc6ad2cf7c36a8804168.png

3. Ajax requests

7c86db67fc1eef848a99adec3e0c9e03.png

4. Ajax response

To access the developer tools in Firefox:

Mac: Access the Tools > Web Developer > Network menu option.

Windows: Click the Firefox menu (top left of the window) and select Web Developer Network option.

The Developer tools are shown at the bottom of the current browser window.

Refresh the page to allow Firefox to capture all requests

The network panel will show all of the requests made by Firefox to load the page.

Click the XHR option at the bottom of the window to reduce the requests to just the Ajax (XHR) requests.

The red glowing button indicates an error occurred.

Further details about the Ajax request are shown.

Double click the Ajax request made by DataTables to view detailed information about the request, including error information.

Please note that these instructions were written with Firefox 26 Windows as the version used. Newer versions, or those on different platforms might have slightly different steps.

IE

1a7644a450a30394ef72607018f4c31d.png

1. Open debugger

e4ab83b4eeaf8f575ac1010816d11de5.png

2. Element inspector

44bb626c1bfaf98db526413aa4fa464d.png

3. Show Network tab

77c622ffeca2ff791310f0a68700d937.png

4. Ajax response

d43260b48d3bd8198e920e73bef979d0.png

4. Ajax response

To access the developer tools in IE:

Press F12 or,

Click the tools menu (top right, a cog icon) and select the F12 developer tools option.

The Developer tools are shown at the bottom of the current browser window. IE will show the document tree by default.

Click on the Network tab to view network requests

IE requires that network traffic be specifically tracked, which we want here.

Click the Start capturing button to capture network requests

Refresh the page to allow IE to capture all requests

The network panel will show all of the requests made by IE to load the page.

Find the Ajax request made by DataTables - it will likely be at or near the bottom of the request list

In this case IE is showing that the file requested as not found.

Details about the Ajax request are shown.

Double click the request to view detailed information about the request.

Please note that these instructions were written with IE 10 as the version used. Newer versions might have slightly different steps.

Opera

c408587039e330d1702d12a3d9da3f5c.png

1. Open debugger

38ea9bd39641eee143d122423b566878.png

2. Element inspector

fb7f7040706cb41176cf1a6bb9bb7052.png

3. Show Network tab

fe1125df9f643debf6d61d3496c37064.png

4. Ajax response

To access the developer tools in Opera:

Mac: Right click or ctrl click the document and select the Inspect element option.

Windows: Right click the document and select the Inspect element option.

The Developer tools are shown at the bottom of the current browser window. Opera will show information about the element that was clicked on.

Click on the Console tab to view console messages logged by Opera

The error console is shown, with logged errors displayed

In this case it is showing that the requested file cannot be found.

Detailed information about the Ajax request can be found in the Network tab.

Click the Network tab in the developer tools

Reload the page to ensure Opera can capture all requests for the page

Locate the request which is in error and double click it to view detailed information.

Please note that these instructions were written with Opera 18 Mac as the version used. Newer versions, or those on different platforms might have slightly different steps.

Resolution

Once you have followed the instructions above you will know what error the server is responding with for the Ajax request.

The most common errors are:

404 - Not Found.

The URL requested was not found on the server.

Check for typos in the file name in the ajax parameter and in your file on the server.

500 - Internal Error.

The server encountered an error while responding to the request.

Check the server's error log for information as to why this error occurred.

Origin not allowed by Access-Control-Allow-Origin.

A request has been made to page on a different domain from the domain which the current page has been loaded from.

Browsers have security measures built in to prevent loading of external scripts - for example on http://localhost you cannot Ajax load a script from http://google.com without special measures.

If you are using a single domain, ensure you are specifying a relative or absolute path without a domain name (i.e. don't start the URL with http(s)://).

Use JSONP or some other cross domain access technology such as a proxy script on your server.

If you are willing to accept the error (for example if you cannot alter the backend system to fix the error), but don't want your end users to see the alert() message, you can change DataTables' error reporting mechanism to throw a Javascript error to the browser's console, rather than alerting it. This can be done using:

$.fn.dataTable.ext.errMode = 'throw';

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值