Home > Blockchain >  rest api post call from mobile device web page failed
rest api post call from mobile device web page failed

Time:12-11

Below js code, it can be executed from desktop correctly, but "NetworkError" from mobile phone chrome:

$.ajax({
            url: apiurl   '/sys/login',
            type: 'POST',
            async: false,
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            success: (res) => {
                if (typeof res.userName === 'string') {
                    if (res.userName.trim().length > 0) {
                        cs.writeCookie("loginstatus", `T|${res.userName}|${res.userId}|${res.employeeName}|${res.employeeId}`, 2);
                        window.location.href = "/pages/kpis/msignin.html";
                    }
                }
                else {
                    console.log('login fail')
                }
            },
            error: (res) => {
                alert(JSON.stringify(res))
            }
        })

From mobile chrome it reports:

{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://127.0.0.1/sys/login'"

Thanks!

I'm tried a lot like below and etc. but cannot solve it:

async: false,

CodePudding user response:

127.0.0.1 is the loopback IP address. It is the IP address the browser is running on.

Your server is running on your desktop.

When your desktop browser asks for 127.0.0.1 then browser asks the server running on the desktop for the resource.

When your mobile browser asks for 127.0.0.1 then browser asks the server running on the mobile for the resource… but that server doesn’t exist.

Use an IP address for the server that the browser on the mobile can access.

CodePudding user response:

The error you're seeing is typically caused when the browser is unable to establish a connection to the URL specified in the AJAX request. This can happen for a variety of reasons, but some common causes include:

The URL is incorrect or the server is down The browser is not connected to the internet There is a network issue or firewall blocking the connection One possible solution is to add error handling to the AJAX request to display a more helpful error message to the user. You can do this by adding an error property to the options object passed to $.ajax(), like this:

$.ajax({
  // other options here
  error: function(xhr, status, error) {
    // Display an error message to the user
    alert(`Error: ${error}`);
  }
});

This will display an alert with the error message if the AJAX request fails. You can then use this information to troubleshoot the issue further.

Another potential solution is to use the $.ajax() method's timeout property to specify a time limit for the request. If the request takes longer than the specified time, it will be automatically cancelled and the error callback will be triggered. This can help prevent the request from hanging if there is a network issue or other problem that prevents the server from responding.

Here is an example of how to use the timeout property:

$.ajax({
  // other options here
  timeout: 5000, // Set timeout to 5 seconds
  error: function(xhr, status, error) {
    // Display an error message if the request takes too long
    alert(`Error: Request timed out`);
  }
});
  • Related